2

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" ?

Dhaval Panchal
  • 612
  • 4
  • 12
  • 32

2 Answers2

4

Try # 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?

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
1

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() ?

Community
  • 1
  • 1
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55