I need to read the value of particular label inside the div tag using jQuery. Below is the HTML structure.
<div id="myDiv">
<label id="greenLabel">Hello</label>
<label id="redLabel">There you go!</label>
</div>
Can anyone help me to find the inner text of "redLabel" ?
I need to read the value of particular label inside the div tag using jQuery. Below is the HTML structure.
<div id="myDiv">
<label id="greenLabel">Hello</label>
<label id="redLabel">There you go!</label>
</div>
Can anyone help me to find the inner text of "redLabel" ?
Share Improve this question asked Nov 22, 2013 at 13:42 Dhaval PanchalDhaval Panchal 6124 gold badges12 silver badges32 bronze badges 1-
Maybe
$('#redLabel').html()
? – Florent Commented Nov 22, 2013 at 13:43
2 Answers
Reset to default 4Try # id-selector
$('#redLabel').text();
or
$('#redLabel').html();
$('#redLabel')
-->refers to element with id
redLabel
$('#redLabel').text();
--> get text of element with id
redLabel
$('#redLabel').text('value');
--> set text of element with id
redLabel
.text()
.html()
ID
must be unique. Use classes
for multiple items or refer to a group
Read Two HTML elements with same id attribute: How bad is it really?
try this
$("#myDiv #redLabel").text();
//go to div then label
// or can try if in label there is no element
$("#myDiv #redLabel").html();
you can also use $("#redLabel")
at the place of $("#myDiv #redLabel")
see also What is the difference between jQuery: text() and html() ?