tags:

views:

52

answers:

3
<span>
    <label for="305">
        <input type="checkbox" name="305" style="vertical-align: middle;" value="305" id="305"> Farming General
    </label>
</span>

How do I get text "Farming General" using jQuery. Please do not ask me to change HTML structure

A: 

.text

Shubham
+7  A: 

You can grab it using an ID selector, going to the .parent() and using .text() like this:

​$("#305").parent().text()
//or:
$("label[for='305']").text();

Though, IDs starting with a number aren't valid until HTML5, just be aware :)


You can also get it without jQuery, like this:

document.getElementById("305").nextSibling.nodeValue

Though you may want to trim it down with $.trim(), like this:

$.trim(document.getElementById("305").nextSibling.nodeValue)
Nick Craver
​$("#305").parent().text() This is more feasible to my current function thank you
geocine
A: 

This is the simplest I think.

$("label[for='305']").text();

Strelok