I'm trying to get a nearby input element with the closest() function, but it says clostest is not a function...
Here is a JSFidlle: http://jsfiddle.net/1mvc76fv/
HTML:
<div>
<input type="text" value="1"/>
<span onClick="fireIt(this)">Kick</span>
</div>
<div>
<input type="text" value="2"/>
<span onClick="fireIt(this)">Kick</span>
</div>`
JS:
window.fireIt = function(param) {
tmp = param.closest('input');
alert( tmp.val() );
}
How to get the closest input element? Why is closest not working?
Use .prev()
method to access the previous input
window.fireIt = function(param) {
tmp = $(param).prev('input');
alert( tmp.val() );
}
Since you're already using jquery, you can use it to bind the event handler rather than using inline handlers as follows:
$("span").click(function() {
tmp = $(this).prev('input');
alert(tmp.val());
});
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.