简体   繁体   中英

How to find particular label value inside div tag using jQuery

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM