Prev: ISO 8601 date format
Next: Yellowfin 5.0 release
From: Andrew Koptyaev on 22 Apr 2010 04:30 I have next piece of code. I have a question - why text "Months" not in there option line of form. Just after form. I is not right for me. I want in there option line. var oOptionIntervalInputM = cElement('option'); cAttribute(oOptionIntervalInputM,'value','m'); var oOptionIntervalInputTextM = document.createTextNode('Months'); oOptionIntervalInputM.appendChild(oOptionIntervalInputTextM); function cElement(el) { this.obj = document.createElement(el); return this.obj; } function cAttribute(obj,att,val) { obj.setAttribute(att,val); return this.obj; }
From: Stefan Weiss on 22 Apr 2010 08:42 On 22/04/10 10:30, Andrew Koptyaev wrote: > I have next piece of code. > I have a question - why text "Months" not in there option line of form. > Just after form. I is not right for me. I want in there option line. > > var oOptionIntervalInputM = cElement('option'); > cAttribute(oOptionIntervalInputM,'value','m'); > var oOptionIntervalInputTextM = document.createTextNode('Months'); > oOptionIntervalInputM.appendChild(oOptionIntervalInputTextM); > > function cElement(el) { > this.obj = document.createElement(el); > return this.obj; > } > > function cAttribute(obj,att,val) { > obj.setAttribute(att,val); > return this.obj; > } That's about as awkward as it can possibly get... and you didn't even add the option to a <select> element yet. You could replace all of this with: var select = document.forms[0].elements.mySelect; // or similar select.options[select.options.length] = new Option("Months", "m"); Furthermore, your cElement() and cAttribute() only work by coincidence, if they work at all. cElement() will create a new *global* property called "obj". You probably wanted function cElement(el) { var obj = document.createElement(el); return obj; } or, simpler: function cElement(el) { return document.createElement(el); } And that's so short that you could just as well write document.createElement("option") directly instead of cElement("option"). The second function, cAttribute, sets an attribute on its "opt" argument, but then returns something else - the global "opt" property created earlier. You probably meant: function cAttribute(obj,att,val) { obj.setAttribute(att,val); return obj; } Which means you could drop this function, too, and just use oOptionIntervalInputM.setAttribute('value','m') directly. Why your code didn't work as intended is not obvious from what you posted. As roundabout as it is, it would probably "work" (in an unsafe way). The error you describe might be caused when you try to add the new option to a select element. Try the "new Option" suggestion (first code snippet), and be careful about how you use "this". -- stefan
|
Pages: 1 Prev: ISO 8601 date format Next: Yellowfin 5.0 release |