Hi guys. I've got some javascript which inserts some code into a text box when a button is clicked. This works fine, but atm I have to define the textbox name within the function. What I'd like to do is to be able to pass that as a variable to the function, so it will insert the text into a specified textbox. What I've currently got: Code: <input type="button" value=" Insert " onclick="insert(this.form,'TEXT')" title="Insert"> When clicked, calls: Code: function insert(theform,format) { theform.textbox.value += format +" "; theform.textbox.focus(); } Hope I've made some sort of sense. Cheers for any help. Hwu
Javascript is such a pain in the ass isn't it? I think what you have to do (I can't conveniently test right now--I'm building apache/php/mysql) is to use the string operators to concatenate the variable into the object reference. document. + "theform" + .textbox.focus(); as I recall. Terribly ugly stuff.
How about adding an id=foo attribute to the fields and using getElementById(foo), that strikes me as something that would work without causing as much violence (exercise fewer Javascript aggravations). I was looking through some of my code for a working example and I came across this little nugget that shows why I love Javascript so damn much: Code: // this is really ugly. // why couldn't JavaScript just have a // decent escaping mechanism or alternative // quote syntax? this is so FUBARed it makes // my eyes bleed. function create(inmate) { // a link to nowhere that shows your popup DIV when // you mouseOver, and shows your popup2 DIV when you // click. var anchor = '<A href="javascript:foo()" onMouseover="show('; anchor += "'" + inmate.popup2 + "'"; anchor += ')" onMouseout="hide('; anchor += "'" + inmate.popup2 + "'"; anchor += ')" onClick="hide('; anchor += "'" + inmate.popup2 + "'"; anchor += '); persist = (persist? false : true); persist? show('; anchor += "'" + inmate.popup + "'"; anchor += ') : hide('; anchor += "'" + inmate.popup + "'"; anchor += ')">*</A>'; var popupcontent = inmate.name; if (inmate.email.length > 1) { popupcontent += '<BR><A href="mailto:' +inmate.email + '">'; popupcontent += inmate.email + '</A>'; } if (inmate.url.length > 1) { popupcontent += '<BR><A href="' +inmate.url + '" target="_BLANK">'; popupcontent += inmate.url + '</A>'; } // all that ugliness and degraration is behind us, so let's // spawn some shiny new DIVs: spawndiv(inmate.name, anchor); spawnpopupdiv(inmate.popup, popupcontent); spawnpopupdiv(inmate.popup2, inmate.name); // and call the function that positions them place(inmate) // if you're still following along after all that, // I'd just like you to know I salute you. }