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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -