HTML
<div>my content of the div1
<input/>
</div>
<div>my content of the div2
<input/>
</div>
<div>my content of the div3
<input/>
</div>
JS
$('input').on('change',function(){
var x = $('this').parent('div').val();
alert(x);
});
My problem is why cannot I get the content of the div which the input was changed ?
Corrected Script
val()is for form fields onlythisshould not be quoted.Alternative
If you only want the text of the parent, you can use
text(). However, browsers can differ on how they implement it (see the notes here). In this case, it’s probably cleaner to wrap the content you want in another element, like:And use script like: