bobo2000
bobo2000

Reputation: 1877

How to get inner html based on attribute Id

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

Answers (5)

Amit Malakar
Amit Malakar

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

Katyoshah
Katyoshah

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

Daniel Apt
Daniel Apt

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

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

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

$('#5').html();

Upvotes: 1

Vaibhav J
Vaibhav J

Reputation: 1334

Try this,

$('#LocationRadioButtons a[id$="5"]').text();

Upvotes: 0

Related Questions