Multi
Multi
Simply put: Getting the user's name
Topic Started: Thursday, 7. July 2011, 14:05 (380 Views)
Joe
Member Avatar
Hi.
[bbc_code]$('div#top_info strong a').text();[/bbc_code] This snippet will simply grab the username of who is currently logged in. For example, if I ran that, it would return "Joe" as a string. You could also make it a nice little variable, though not necessary, like so.[bbc_code]var username = $('div#top_info strong a').text();[/bbc_code] With that, let's create a small `if` statement for a simple match.[bbc_code]if ($('div#top_info strong a').text() == 'Joe') {
alert('Hi Joe!'); // Yay, it's a match!
} else {
alert('You\'re not Joe!'); // Looks like it wasn't a match..!
}[/bbc_code] * Simply states the element is a div, and to look only for divs - nothing else. Try to include this in your coding. Efficiency, yes!
* The div id is `top_info`, thus we used a `#` symbol instead of a period.
* The text we're looking for - the username - has the `<strong>` tags around it. Perfect!
* The text we're looking for is also a link, so we use `a` to narrow things down even more.
* Simply returns what we found as a string.

Also, if you went with the variable option.[bbc_code]if (username == 'Joe') {
alert('Hi Joe!'); // Yay, it's a match!
} else {
alert('You\'re not Joe!'); // Looks like it wasn't a match..!
}[/bbc_code]
Joe
Off
Profile
Quote
Top
 
Oracle
Member Avatar
Level 8
Why do you include the \ in the 'You\'re'?
Visit my shop! Forum reviews, posting packages, recolors
Posted ImagePosted Image
Posted Image
Me!

Off
Profile
Quote
Top
 
Sith...
No Avatar
Level 4
Oracle
Friday, 8. July 2011, 04:52
Why do you include the \ in the 'You\'re'?
So apostrophe is displayed and the string doesn't end, I'd assume...
Edited by Sith..., Friday, 8. July 2011, 07:19.
Off
Profile
Quote
Top
 
Oracle
Member Avatar
Level 8
I guess so, thanks. :)
Visit my shop! Forum reviews, posting packages, recolors
Posted ImagePosted Image
Posted Image
Me!

Off
Profile
Quote
Top
 
Aidan
Member Avatar
Level 12
Outline Documentations
T H E M E S

Documentation accepted in to the Themes database
Off
Profile
Quote
Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Coding & Development · Next Topic »

Welcome Guest [Log In] [Register]
Outline Live
Loading..
Loading..