0

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

bobo2000
  • 1,779
  • 7
  • 31
  • 54

5 Answers5

1

while Id is unique for this element you can directly use id to get html

$('#5').html();
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
1

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.

Community
  • 1
  • 1
Daniel Apt
  • 2,468
  • 1
  • 21
  • 34
0

Try this,

$('#LocationRadioButtons a[id$="5"]').text();
Vaibhav J
  • 1,316
  • 8
  • 15
  • Thanks, instead of hardcoding the id, how do I dynamically set the Id using a variable; $('#LocationRadioButtons a[id="'+currentId+'"]').html() doesnt work – bobo2000 Dec 12 '14 at 12:05
  • $('#LocationRadioButtons #' + currentId).html(); ID's are unique, so you dont need to prefix it with the 'a' tag – atmd Dec 12 '14 at 12:15
0

an id is unique so you can just use the id selector to select an element with a specific id like this:

 $('#5').html();
Katyoshah
  • 129
  • 10
-1

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() );
    }
});
Amit Malakar
  • 618
  • 1
  • 5
  • 10