Any ideas, for a neat workaround (maybe without try-catch)?
Gregor
[1] http://msdn.microsoft.com/en-us/library/ms534696(VS.85).aspx
> Trying to create a button element in IE with type set to button. The
> latter thing is required since
> "In IE8 mode, the default value is submit" [1]
> However, trying to set type to "button" (even when the element is *not*
> attached to the document tree), causes "object does not support this
> action(?)" (I do run a German IE8 and it says "Aktion" not something
> like "value" or "property").
>
> Any ideas, for a neat workaround (maybe without try-catch)?
Supposing you use Javascript,
please show the minimal code that gives your problem.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Button elements have always been problematic in IE, but not being able
to set the - type - property is correct according to (HTML DOM level
2) spec (it is specified as read-only), even if that is poor spec
design on a element that could have 3 types and provides no
alternative setting mechanism.
Except the Core DOM does offer setAttribute, and strangely (given IE's
odd attitude to setAttribute):-
window.onload = function(){
var el = document.createElement('BUTTON');
el.setAttribute('type', 'button');
var st = el.outerHTML;
el.innerHTML = st.replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(el);
};
- in IE 6 and 7, gives the impression of having created a button
element with a type of 'button'.
Richard.
I suppose
var elem = document.createElement("button");
elem.type = "button";
should suffice.
Gregor
> On Nov 19, 9:52 am, Gregor Kofler wrote:
>> Trying to create a button element in IE with type set to
>> button. The latter thing is required since
>> "In IE8 mode, the default value is submit" [1]
>> However, trying to set type to "button" (even when the element
>> is *not* attached to the document tree), causes "object does not
>> support this action(?)" (I do run a German IE8 and it says
>> "Aktion" not something like "value" or "property").
>>
>> Any ideas, for a neat workaround (maybe without try-catch)?
>
> Button elements have always been problematic in IE, but not being able
> to set the - type - property is correct according to (HTML DOM level
> 2)
I see. According to my completely unscientific test it seems, that all
other (popular) browsers allow to set the type. And agreed: it is indeed
completely idiotic to have a read only property with 3 possible states,
which cannot be set...
> Except the Core DOM does offer setAttribute, and strangely (given IE's
> odd attitude to setAttribute):-
Yes, that seems an alternative.
Gregor
> Evertjan. meinte:
>> Gregor Kofler wrote on 19 nov 2009 in comp.lang.javascript:
>>
>>> Trying to create a button element in IE with type set to button. The
>>> latter thing is required since
>>> "In IE8 mode, the default value is submit" [1]
>>> However, trying to set type to "button" (even when the element is
>>> *not* attached to the document tree), causes "object does not
>>> support this action(?)" (I do run a German IE8 and it says "Aktion"
>>> not something like "value" or "property").
>>>
>>> Any ideas, for a neat workaround (maybe without try-catch)?
>>
>> Supposing you use Javascript,
>> please show the minimal code that gives your problem.
>
> I suppose
So you are not sure it gives your problem?
> var elem = document.createElement("button");
> elem.type = "button";
>
> should suffice.
Does that give your problem?
I don't think so, it would be invisible,
as it is not set into the dom-tree.
>>> please show the minimal code that gives your problem.
>> I suppose
>
> So you are not sure it gives your problem?
It does. Though it's probably not too useful to copy excerpts from my
library.
>
>> var elem = document.createElement("button");
>> elem.type = "button";
>>
>> should suffice.
>
> Does that give your problem?
Yes.
> I don't think so, it would be invisible,
> as it is not set into the dom-tree.
It doesn't matter whether it's attached to the DOM-tree.
Fixed by replacing it with
button.setAttribute("type", "button");
which works in both IE and non-IE browsers.
Gregor
>>> var elem = document.createElement("button");
>>> elem.type = "button";
>>>
>>> should suffice.
>>
>> Does that give your problem?
>
> Yes.
>
>> I don't think so, it would be invisible,
>> as it is not set into the dom-tree.
>
> It doesn't matter whether it's attached to the DOM-tree.
>
> Fixed by replacing it with
> button.setAttribute("type", "button");
> which works in both IE and non-IE browsers.
>
If it is not attached to the dom-tree,
how can an element be visible?
But I agree, the type can be valid without the attachment.
var elem = document.createElement('button');
elem.type = 'button'; // line 2
elem.innerText = 'blah';
//document.getElementById('anElem').appendChild(elem);
alert(elem.type) // alerts "submit" in Chrome
So elem.type seems readonly, as tested in G-Chrome.
and gives an action not supported for line 2 error for IE8
=========================
So why do you need a button type button again?
If it is not attached to the DOM
[or to a orphan form element ?],
it possibly cannot know if it is inside a <form> scope?
It has nothing to do with "visibility". Creating a button it becomes
automatically a submit button, i.e. type is set to "submit" and cannot
be changed directly.
> var elem = document.createElement('button');
> elem.type = 'button'; // line 2
> elem.innerText = 'blah';
> //document.getElementById('anElem').appendChild(elem);
> alert(elem.type) // alerts "submit" in Chrome
>
> So elem.type seems readonly, as tested in G-Chrome.
> and gives an action not supported for line 2 error for IE8
Yes, that's what I figured out, too.
> =========================
>
> So why do you need a button type button again?
Because I need a button button, not a submit button. Anyway, the
interesting part was, why assigning a value to the type property gives
me a "not supported action" in IE.
> If it is not attached to the DOM
> [or to a orphan form element ?],
> it possibly cannot know if it is inside a <form> scope?
Yes, it probably wouldn't make a difference.
Gregor
Indeed.
You do not need a submit button to submit a form,
as myForm.submit() will do the trick.
Evenso what function would an invisible button button have,
that cannot be done with an invisible text type input?
Or even an visible button type button, as that function could be done
wtih a submit type button that is onclick precented from submitting.
============
But I do not doubt for a moment, that this is bad programming of IE8,
as the readonly way of Chrome is a bit friendlier.
Another option is to use an input type=button.
--
Rob