Monday, 15 April 2013

Jquery for nested element -



Jquery for nested element -

i have div construction below (pls check image). wanted alter content of text box when click on relative button using jquery.

for example, if click button (1), content of textbox (1) should change. if click button (2) content of textbox (2) should change.

i have written next code, working button(2), when click button(1) changes content of both textbox.

$(document).on('click', '.b', function(e) { var = $(this).closest('.a'); $('.c', a).val("sample text"); });

i can solve having different classes different button / textbox. wanted have same class construction , solve problem.

if dom construction same in image, should work.

html:

<div class="a"> <input type="text" class="c"> <input type="button" value="button" class="b"> <div class="a"> <input type="text" class="c"> <input type="button" value="button" class="b"> </div> </div>

jquery:

$('.b').on('click', function () { //find parent .a var $a = $(this).parent(); //add in text $a.children(".c").val("sample text"); });

working example

jquery

No comments:

Post a Comment