jquery - How to create form elements input box , label using javascript onclick -
i know how create hidden type input box , labels using javascript onclick function.
$().ready(function() { $("#product").autocomplete("get_completeproducts", { width: 406, matchcontains: true, selectfirst: false }); }); <input type="text" value="" name="product" id="product" size="50"> <input type="button" value="add list" onclick="addprotolist()" name="select_pro">
i getting above input box value through ajax auto complete , user click on select box , have , have add list them number of selected phones bellow input box like
<label>samsung galaxy y<label><input type="hidden" value="778" name="pro_id[]"> <label>samsung galaxy y duos<label><input type="hidden" value="788" name="pro_id[]"> <label>samsung galaxy y plus<label><input type="hidden" value="728" name="pro_id[]"> . . . <input type="submit" name="save" value="save list">
anybody can provide me how code using javascript or jquery.
function addprotolist() { var str = '<label>'+ var_for_text +'</label><input type="hidden" value="'+ var_for_value +'" name="pro_id[]">'; $('#yourform').append( str ); }
note
if want append multiple input
, label
etc using loop, don't call append within loop, slow down performance. make string above , call .append()
@ last out of loop.
for example
var str = ''; function addprotolist(inputobj) { // typical example of inputobj may // inputobj = [ {labeltext: 'some 1', value: 'val1' }, {labeltext: 'some 2', value: 'val2'} ] // loop for(var = 0; < inputobj.length; i++ ) { str += '<label>'+ inputobj[i].labeltext +'</label><input type="hidden" value="'+ inputobj[i].value +'" name="pro_id[]">'; } // call append outside of loop $('#yourform').append( str ); }
but way not make sense use label
. have connect input
either wrapping or specifying for
attribute pointing input's id
:
<!-- simple label example attribute --> <input type="radio" name="clickmebutton" id="clickmebutton"> <label for="clickmebutton">click me</label> <!-- or more --> <label><input type="radio" name="clickmebutton"> click me</label>
from label
on mdn
Comments
Post a Comment