PDA

View Full Version : <fieldset> undefined?



Ruberto
02-02-2007, 09:43 PM
I came across a bug while implementing my field copy script. On a particular form I kept getting an "undefined error". I also noticed that the values in the editable fields were all offset from the way they should have rendered (i.e. the bottom value in the top field). After some head scratching I discovered the deviant. It is the <fieldset></fieldset> tag. Javascript sees it as a form element but doesn't seem to know what it is. The element.type method returns "undefined". I cannot find anything about this in the JS documentation. Anybody know how to deal with this problem?

If I cannot find a way to trap out the errant object then I'll have no choice but to stop using <fieldset> in my forms.

Thanks!

Ruberto

Twey
02-02-2007, 09:54 PM
On what browser?

mburt
02-02-2007, 09:54 PM
element.type doesn't say what an element is. typeof element declares what it is, if type == null, it will obviously return an error.

Twey
02-02-2007, 10:03 PM
If one has:
<input type="password" id="pass">... then document.getElementById("pass").type is "password" :)

mburt
02-02-2007, 10:49 PM
I think the OP got the wrong impression of the "type" attribute. He/she probably thought it was a method which returned a value of what type the element being tested was. Again, a simple error :)

mburt
02-02-2007, 10:50 PM
By the way, there isn't any ways to dynamically change the "type" attribute, that I know of.

Twey
02-02-2007, 11:56 PM
I think the OP got the wrong impression of the "type" attribute. He/she probably thought it was a method which returned a value of what type the element being tested was.For form elements, it does.

Admittedly it's a property, not a method, but I don't think Ruberto has the wrong idea.

jscheuer1
02-03-2007, 05:54 AM
I came across a bug while implementing my field copy script. On a particular form I kept getting an "undefined error". I also noticed that the values in the editable fields were all offset from the way they should have rendered (i.e. the bottom value in the top field). After some head scratching I discovered the deviant. It is the <fieldset></fieldset> tag. Javascript sees it as a form element but doesn't seem to know what it is. The element.type method returns "undefined". I cannot find anything about this in the JS documentation. Anybody know how to deal with this problem?

If I cannot find a way to trap out the errant object then I'll have no choice but to stop using <fieldset> in my forms.

Thanks!

Ruberto

Well, you haven't specified where this comes up in the code but, there should be a relatively easy way around it. A fieldset has no type attribute but, some browsers might think that it does. So, you need to find a way to either skip it gracefully or to count it either for for what it is or as just something in the form. For example:


if (element.type||(element.tagName&&element.tagName.toLowerCase()=='fieldset'))

will return true if the element has a type attribute and/or is a fieldset. While:


if (element.type&&(element.tagName&&element.tagName.toLowerCase()!='fieldset'))

will return false for a fieldset even if the browser thinks it has a type attribute.

Other tests can be devised to deal with fieldsets in whatever way you decide that you need to.

Twey
02-03-2007, 03:08 PM
I'd still like to know in what browser(s) this occurs.

Ruberto
02-03-2007, 07:24 PM
The code is here in another thread:
http://www.dynamicdrive.com/forums/showthread.php?t=17078

You'll need to put the code on a page with a form containing an equal number of hidden and editable form elements. Uncomment the line with //alert(widgetType) which you'll see in the new populateFields() function at the bottom of the posting. You may use this page for a test but it does not have the new populateFields() function.

http://frankentron.freezope.org/update.html

I guess it isn't necessary you can use that version the way it is but add some <fieldset> tags and add the line to popup the value of the widgetType variable.

The <fieldset> tag makes the script break in both Firefox and IE.

Twey is right I mistakenly wrote method when I should have said property.

I like John's idea for dealing with this problem, I'm gona give that a try.

Thanks everyone for the help and instruction. I'll let you know how it turns out later.

Ruberto
02-07-2007, 03:06 PM
Well I had to direct my attention to some other things the past couple days. I'm back to this though and there just does not seem to be any way to trap this tag out that I can find. I tried Johns idea with no joy and some other ideas I came up with, In the end I decided to simply pop the element out of my array. Considering the <fieldset> tag is always the first thing in a form it's just a matter of removing the first item from my editFields array variable. Using the handy shift() method I just eliminate it from the array altogether and voila the script works nicely again. e.g.


fieldset = editFields.shift()

the fieldset variable is then emptied as it serves no purpose other than to catch the value returned from the shift() method.

I really appreciate all the helpful suggestions and advice! Thanks!

BTW: This problem oddly does not appear with the <legend> tag.

smorelli
02-08-2007, 12:19 AM
So how would you apply that to these functions for a form that contains fieldsets?


<script>

//"Accept terms" form submission- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use

var checkobj

function agreesubmit(el){
checkobj=el
if (document.all||document.getElementById){
for (i=0;i<checkobj.form.length;i++){ //hunt down submit button
var tempobj=checkobj.form.elements[i]
if(tempobj.type.toLowerCase()=="submit")
tempobj.disabled=!checkobj.checked
}
}
}

function defaultagree(el){
if (!document.all&&!document.getElementById){
if (window.checkobj&&checkobj.checked)
return true
else{
alert("Please read/accept terms to submit form")
return false
}
}
}

</script>

jscheuer1
02-08-2007, 04:21 AM
I'd like to see the entire page, or at least enough of it to see the problem in action but, I'd try:


if(tempobj&&tempobj.type&&tempobj.type.toLowerCase()=="submit")

smorelli
02-09-2007, 09:30 AM
John - It still doesn't seem to work. Now it is saying that the object is expect. Have a look at the working page.

http://www.pyproduction.com/halo2/agree-test.html

jscheuer1
02-09-2007, 02:17 PM
function agreesubmit(el){
checkobj=el
if (document.all||document.getElementById){
for (i=0;i<checkobj.form.length;i++){ //hunt down submit button
var tempobj=checkobj.form.elements[i]
if(tempobj&&tempobj.type&&tempobj.type.toLowerCase()=="submit")
tempobj.disabled=!checkobj.checked
}
}
}

You had a left over (and misunderstood) modification from the earlier discussion, use the above function agreesubmit(el) and it will work fine.

smorelli
02-10-2007, 10:20 AM
Hey John - Thanks a Million!