Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting span value inside div

Let's say I have

    <div id="1">
    <span id="name">Josh</span>
    <div id="quote">Run</div>
    </div>

    <div id="2">
    <span id="name">Mark</span>
    <div id="quote">Run</div>
    </div>

How would I select the name between the span tags from the first div?

$("#quote").click(function(){

    var name = $('#name').html();

}); 
like image 897
domino Avatar asked Jun 22 '26 09:06

domino


1 Answers

Firstly, id's have to be unique in an html document. So,ideally, you need to change those id's within the div to a class. Secondly, id's cannot begin with a numeric, so you need to change your div id's to start with a letter or underscore.

With that in mind, given this structure:

<div id="one">
    <span class="name">Josh</span>
    <div class="quote">Run</div>
</div>

<div id="two">
    <span class="name">Mark</span>
    <div class="quote">Run</div>
</div>

you would be looking for:

$("#quote").click(function(){

    var name = $('#one .name').html();

}); 
like image 81
Jamiec Avatar answered Jun 23 '26 22:06

Jamiec