Monday, 15 August 2011

jquery - Changing multiple same name input from different parent -



jquery - Changing multiple same name input from different parent -

just several questions here

in case if have form, construction below

<form> <div id='dynamicformbox'> <div id='parent_1'> <div id='level_2'> <div id='level_3'> <input id='name' name='name' value=''/> <input id='id' name='id' value=''/> <input id='passno' name='passno' value=''/> <input type="button" id="removediv"/> </div> </div> </div> <div id='parent_2'> <div id='level_2'> <div id='level_3'> <input id='name' name='name' value=''/> <input id='id' name='id' value=''/> <input id='passno' name='passno' value=''/> <input type="button" id="removediv"/> </div> </div> </div> <div id='parent_3'> <div id='level_2'> <div id='level_3'> <input id='name' name='name' value=''/> <input id='id' name='id' value=''/> <input id='passno' name='passno' value=''/> <input type="button" id="removediv"/> </div> </div> </div> </div> </form>

so questions here are

how to, input 'name' either 1 of parent? , in case if input on 'name', can tell 'name' comes parent id , not other parent? how want change, input 'id' 3 parents, different value each input 'id'? how create if alter of input 'id', simultaneously changed parent id too? if can add together parent div using append, how create id of parent changed according number of children within dynamicformbox? right now, have parent_1, parent_2 , parent_3, if append next parent, id automatically set parent_4? how create that, 'parent' part of parent id next value of input 'id'? parent_1, if changed value of input 'id' 'john', parent id alter 'john_1'? how create that, 'number' part of parent id next value of input 'passno', , @ same time changing number format? example, parent_2, if changed value of input 'passno' 2, alter parent id parent_02? for delete button id 'removediv', if button goes same removediv() function if click it.. , clicked button within parent_2, how can create sure delete parent_2 block, , not others?

i know of questions have been answered in here, want know case inputs shared same name , same id, want know how differentiate input in respect belonging parent.

how to, input 'name' either 1 of parent? , in case if input on 'name', can tell 'name' comes parent id , not other parent?

this homecoming value input in parent_3

var value = $('#parent_3 input[name=name]').val(); how want change, input 'id' 3 parents, different value each input 'id'?

just utilize loop:) it's you, need implement right select quesry that:

lets define new function that:) parentid , json new field values. can set whole form 1 function call, need create json obj.

var newvalues = { name: 'new name value', 'passno': 'new_value' } function setval(parentid, newvalues){ if(newvalues.name) $('#'+parentid+' input[name=name]').val(newvalues.name); if(newvalues.passno) $('#'+parentid+' input[name=passno]').val(newvalues.passno); }

if want alter input 3 parents, lets name input, use:

$(input[name=passno]').val(newvalue)

3.how create if alter of input 'id', simultaneously changed parent id too?

if nwant event trigger on value alter utilize alter method, so:

$('#'+parentid+' input[name=passno]').change(function(){ console.log('new value ' + $(this).val()); }); but not recomment alter ids of dom. it's bad practise.

5.how create that, 'parent' part of parent id next value of input 'id'? parent_1, if changed value of input 'id' 'john', parent id alter 'john_1'?

$('#parent_1 input[name=name]').change(function(){ var parentnode = $(this).closest('.parent-block'); var newid = newid($(this).val(), parentnode); // homecoming new id.(you have write ofc:)) because subject of next point. parentnode.attr('id',newid ); // need add together class 'parent-block' parents. because id alter need find way find parent. });

when in onclick function $(this) jquery obj clicked object, in case button. utilize jquery functions parent. there 2 known me ways, either parent() <-go el parent, or closest() <-goes fist node (ancestor) given qualifier, css class.

$('button').on('click', function()){ $(this).parent().parent().parent().remove(); $(this).closest('.parent-block').remove(); // need add together class parent blocks:)

};

jquery

No comments:

Post a Comment