javascript - How to change placeholder and name from select option dynamically using jQuery -


if need edit question please state before down voting.

i'm creating form when option selected. form appear depends on option selected. how dynamically change placeholder , name. if option selected multiple times placeholder , name increment like

placeholder="holder1" name="name1"  placeholder="holder2" name="name2" 

here's code:

twig file

<select class="form-control" id="collaterals" name="loan_type_id" required>     <option disabled value="0" selected}>-- select collateral --</option>     <option value="" id="veh_coll">vehicle</option>     <option value="" id="hal_coll">house , lot</option>     <option value="" id="lot_coll">lot</option> </select> 

jquery

jquery(document).on('click',"#removethis",function(e){     jquery(this).closest('tr').remove();     e.preventdefault(); });  jquery("#addtype").click(function() {     var selectedtype = jquery('#collaterals :selected').text();     jquery("#nfo").clone().appendto('tbody');     jquery("#addtypetext").val(selectedtype);     jquery("#nfo").removeclass("hidden-xs-up"); }); 

works fine when press add button

enter image description here

i need placeholder , name dynamic depends on option selected , making name increment example name="vehicle1" if add option name="vehicle2"

i self-taught please show me better way if possible.

create counter global scope:

i = 0  

(inside function)

window[i] = 0  

increment when choose option:

window[i] ++ 

when want set name , placeholder:

$(input).attr("placeholder", newvalue + window[i]) $(input).attr("name", newvalue + window[i]) 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -