javascript - Jquery closest() doesn't work on elements within different div -
i'm trying use closest()
<input>
element in different div. here's markup:
<div class="row"> <div class="span8"> <input type="text" disabled="disabled" id="image-name"> </div> <div class="span4"> <input type="file"> </div> </div>
here's script
$(document).ready(function(){ $("input:file").change(function (){ var filename = $(this).val(); //put file name inside disabled <input> $(this).closest('input:disabled').val(filename); }); });
it doesn't though. tried changing input:disabled
#image-name
still doesn't work.
any solution?
thanks
if have id on other input element, why using closest function? why not $('#image-name')? closest method not work way think. closest goes dom tree until finds match.
based on comment initial suggestion, dry using more fancy versions of jquery event binders:
function handler(e, args) { $(e.data.elem).val(filename); } $('input:file').bind('change', { elem: '#image-name' }, handler); $('#other-input').bind('change', { elem: '#other-field' }, handler);
reusable event handler parameterized using event data constructs.
Comments
Post a Comment