javascript - Trying to append the textfield value to another textfield -
i doing basic javascript choosing value popup , value appearing in textfield. there button on side of textfieldfield, when clicked transfer value textfield comma separated.
i mean new textfield have values comma separated , not replaced.
i doing code
<input type="text" class="inputs" style="width:70px;" name="color1" id="color1" value="" maxlength="7" size="7"> <a href="javascript:addtotextfield('color1')"><img src="icon_add.gif" alt="add text box above" title="add text box above" border="0"></a> function addtotextfield(cfieldname) { var objtxt = document.getelementbyid('sta'); objtxt.appendchild(cfieldname); }
another text field need pass value
<input type="text" name="sta" id="sta" class="inputs" />
textbox element can't have child nodes, got value. append value of other textbox comma delimeted string, have this:
function addtotextfield(cfieldname) { var ofield = document.getelementbyid(cfieldname); var valuetoadd = ofield.value; if (valuetoadd.length === 0) return; //don't add empty values var objtxt = document.getelementbyid('sta'); var existingvalues = (objtxt.value.length === 0) ? [] : objtxt.value.split(","); existingvalues.push(valuetoadd); objtxt.value = existingvalues.join(","); ofield.value = ""; //clear textbox ofield.focus(); //bring focus }
the above clear sender textbox value , re-focus next input.
Comments
Post a Comment