Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change <button>'s "type" with Javascript

On Chromium 7.0.517.44 (64615) Ubuntu 10.10, I seem to be unable to change the type attribute of a <button> element:

> blah = document.createElement("button")
  <button>​</button>​
> blah.type
  "submit"
> blah.type = "button"
  "button"
> blah.type
  "submit"

Help?


On Firefox 3.6.12 and Opera 10.63, it works fine:

>>> blah = document.createElement("button")
    <button>
>>> blah.type
    "submit"
>>> blah.type = "button"
    "button"
>>> blah.type
    "button"
like image 910
erjiang Avatar asked Nov 13 '10 02:11

erjiang


2 Answers

Use setAttribute.

blah.setAttribute('type', 'button');
like image 73
PleaseStand Avatar answered Oct 28 '22 07:10

PleaseStand


Change the type attribute on any of the input family generally doesn't work (I know for a fact it does not work on input).

What you may want to do is clone the element, replace the type attribute whilst the HTML is serialised and then append it after. Then delete the original.

like image 31
alex Avatar answered Oct 28 '22 07:10

alex