简体   繁体   中英

Selecting a value from a p tag within a div

I am creating a list of records that is contained within divs, I want to get the value of the contents within ap tag that is within the div clicked. How do I reference to the p tag and its value with a .click function, using some jquery?

What I have

<div class="result_box">
     <p class="result_text" id="res_id">id# 0000000</p>
     <p class="result_text" id="res_mail">resume@resume.com</p>
     <p class="result_text" id="res_complete">Active</p>
     <p class="result_text" id="res_date">11/11/11</p>
</div>

Javascript

var record       = $(".result_box"); //Fields wrapper

 $(record).click(function(e){ //on add input button click
    //e.preventDefault();
    var record_id = //whatever value is within <p id="res_id">

    alert('button_clicked');
});

Additional info if needed, the class "result_box" is the same throughout, but I just want the value with that specific result box.

Within the div clicked, use:-

var record_id = $(this).children('#res_id').text();

Though, as an Id should be unique, you could just do:-

var record_id = $('#res_id').text();

If you are using multiple #res_id , then change them to classes .res_id and do:-

var record_id = $(this).children('.res_id').text();

children only travels a single level down.

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