Reputation: 1877
I have the attibute Id.
In console when I type in the following jquery command:
$('#LocationRadioButtons a')
I get the following output
[<a id="4" href="#">Test</a>, <a id="5" href="#">Test1</a>, <a id="6" href="#">test2</a>]
Which is an array
If I type in the following jquery command:
$('#LocationRadioButtons a').first();
It will return the first element in that array:
Test
How do I return an element based on it's Id, and return its innerHTML. For example id = 5 innerHTML is test1,
Cheers
Upvotes: 0
Views: 167
Reputation: 608
Try this: As you already have the elements id, just do
$('#5').html();
Will alert Test1
jquery each()
loop is useful when you don't have a selector and you want to parse through each element to check for certain condition.
$('#LocationRadioButtons a').each(function(index, value){
var myattr = $(this).attr('id');
if(myattr=='5') {
alert( $(this).html() );
}
});
Upvotes: -1
Reputation: 129
an id is unique so you can just use the id selector to select an element with a specific id like this:
$('#5').html();
Upvotes: 0
Reputation: 2638
You can get the html by using html()
You can use
$('#LocationRadioButtons #5').html();
Based off your markup you can actually simply use
$('#5').html();
PS: I'd refrain from having ids start with a number. HTML4 doesn't like this.
Upvotes: 1
Reputation: 24001
while Id is unique for this element you can directly use id to get html
$('#5').html();
Upvotes: 1