[Javascript] cant create a button with a label

Hassan Schroeder hassan at webtuitive.com
Tue Mar 29 08:49:08 CST 2005


judah wrote:
> I can't create a button with a label. What am I doing wrong?

Uh, making up non-existent attributes?  :-)

> function createButton() {
>    var button = document.createElement("button");
>    button.value = "Hello Kitty1";
>    button.label = "Hello Kitty2";
** there's no "label" attribute for BUTTON
>    button.style.label = "Hello Kitty3";
>    button.style.value = "Hello Kitty4";
** $DEITY only knows what "style" is supposed to be doing here!
>    button.name = "Hello Kitty5";
>    button.text = "Hello Kitty6";
** no "text" attribute for BUTTON
>    button.type = "button";
>    button.id = "hello";
>    button.style.width = 120+"px";
>    button.style.height = 20 + "px";
>    document.body.appendChild(button);
** you should be appending a BUTTON element to a block element
(DIV, P, FIELDSET) within a FORM, not just randomly in the body.
> }

BUTTON is a non-empty element; try something like this:
    /* bad practice to use an element name as a variable name, BTW */

    var thisButton     = document.createElement("button");
       thisButton.type = "button";
       thisButton.name = "HelloKittyButton";
       thisButton.id   = "HelloKittyButton";
    var thisButtonText = document.createTextNode("Hello Kitty");
    thisButton.appendChild(thisButtonText);
    var thisForm       = document.getElementsByTagName("form")[0];
    var thisFieldset   = thisForm.getElementsByTagName("fieldset")[0];
    thisFieldset.appendChild(thisButton);

HTH!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.





More information about the Javascript mailing list