Forum Moderators: open

Message Too Old, No Replies

IE problem - changing style of dynamically named form

attempt to change dynamically named form gives error

         

sunswept

3:18 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



I have a form whose name is supplied in Javascript code and not in html.
When I try to alter the style.visible property of this form IE gives an error but FF works as expected.
It will seem strange to name the forms after defining them in the same page but for some reasons it has to be done
code:
HTML CODE

<form>
.... form elements
</form>
<form >
.... form elements
</form>
forms[0].name='form1'
forms[0].name='form2'

JAVASCRIPT CODE


foo( formName){
myForm = document.forms[ formName ];
myForm.style.visibility = 'visible';
...(several more operations with myForm)
}

IE gives the error - "myForm.style is null or not an object"
I looked for a solution and discovered that there is a problem with altering attributes of a dynimically created form in IE but was unable to decide what to do.
people have suggested altering outerHtml but i dont understand that.
Is there a simpler way?
Thanks

Bernard Marx

3:51 pm on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That depends what the other operations are. If "visibility" is the only style op., then you could sidestep the problem by wrapping in a DIV.
- I guess you've already thought of that!

sunswept

4:09 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



no visibility is not the only thig, the function does some operations on elements of the form which cannnot be accomplished if i dont have a valid reference to the form

penders

4:24 pm on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does it help if you set an id attribute on the form and access the form via getElementById()?

[edit]

If you are accessing the forms by number in order to set their name attribute, can you not access them by number again later to set their visibility etc....?

sunswept

4:34 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



IT WORKS!
yes it works well in both ie and ff

i read on quirksmode.org that using getElementById for forms might cause problems so i use forms[ '<name?' ]. If you have an idea about when a problem arises pls let me know

thank you penders and bernard

penders

9:32 pm on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it sorted - the id and getElementById() method I assume?

i read on quirksmode.org that using getElementById for forms might cause problems so i use forms[ '<name?' ].

Hhhmmm, I was not aware of this... unless may be you are wanting to support very old browsers with your forms, that don't support the newer DOM methods?! If you simply want to read values from your forms then the forms[] array is fine.

I think I found the page on quirksmode.org that you are referring to (or possibly an updated version), entitled "Extending forms" - regarding the dynamic creation of form fields using JS. However, it seems to suggest the contrary:

PPK:
Only using the W3C DOM you can allow your users to generate as many form fields as they need. This effect is impossible to mimic with any previous JavaScript technique.
:
:
Problems in Explorer
...the generated form fields are unreachable by a traditional document.forms call: Explorer simply doesn't enter them in the arrays. This can be worked around by giving the form field an ID and then using getElementById().

;)