Alex Manzano Alex Manzano - 2 months ago 11
HTML Question

Jquery selecting span is not working

I have this in my code

<div class="prof-infos">Email:&nbsp;&nbsp;&nbsp;<span>blahblah@email.com&nbsp;&nbsp;&nbsp;</span><span class="change">change</span></div>


and I have this

$(".change").click(function() {
alert("bloop");
});


however when I click the span; it doesn't do anything.

SOLUTION

use this instead

$(document).on("click", ".change", function() {
alert("why does this work?");
});

Answer

I thik you have missed jquery library, its working here ,check the above snippet

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

$(".change").click(function() {
   alert("bloop");
});
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<div class="prof-infos">Email:&nbsp;&nbsp;&nbsp;<span>blahblah@email.com&nbsp;&nbsp;&nbsp;</span><span class="change">change</span></div>

you can use like this

$(document).on('click','.change',function(){
   /*Your code*/
});

$(document).on('click','.change',function(){
   alert("bloop");
});
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
    <div class="prof-infos">Email:&nbsp;&nbsp;&nbsp;<span>blahblah@email.com&nbsp;&nbsp;&nbsp;</span><span class="change">change</span></div>