1

the name of the button is not getting displayed... this is the code:

var button = document.createElement('button');
button.setAttribute('name','write');
button.setAttribute('class','ui-button_S40button');  
button.setAttribute('style','width: 100px;');
button.setAttribute('value','Write');  
document.getElementById("write_testi").appendChild(button);
3
  • can u show complete code Commented Jul 4, 2013 at 13:23
  • applying a name this way works fine for me.. jsfiddle.net/uTXBC Commented Jul 4, 2013 at 13:29
  • Did you mix up name and innerHTML ? Commented Jul 4, 2013 at 13:31

7 Answers 7

1

Use :

 button.setAttribute('text','Write');  

or

button.innerHTML = 'Write';
Sign up to request clarification or add additional context in comments.

2 Comments

The attribute name is something different than the buttons innerHTML
An HTML attribute text does not exist. The first example won't work.
1

Try this

  var  button = document.createElement('input');
          button.type = 'button';
          button.value = 'Write';
          button.id ='Write';
          .........................
          .........................


document.getElementById("write_testi").appendChild(button);

1 Comment

Why are you creating an input element?
1

You are getting stumped here.

var button = document.createElement('button');

Try:

var button = document.createElement('input');

2 Comments

@FelixKling because i din't know about that syntax of creating button. can you explain?
1

Try using an input element (see it on jsFiddle):

var button = document.createElement('input');
button.setAttribute('name', 'write');
button.setAttribute('type', 'button');
button.setAttribute('class', 'ui-button_S40button');
button.setAttribute('style', 'width: 100px;');
button.setAttribute('value', 'Write');
document.getElementById("write_testi").appendChild(button);

Or assign the innerText DOM attribute (demo):

var button = document.createElement('button');
button.setAttribute('type', 'button');
button.setAttribute('name', 'write');
button.setAttribute('class', 'ui-button_S40button');
button.setAttribute('style', 'width: 100px;');
document.getElementById("write_testi").appendChild(button);
button.innerText="Write";

1 Comment

The standard property is .textContent, your example will work in IE but not in Firefox.
0

Try this:

var button = document.createElement('button');
    button.className = 'ui-button_S40button';
    button.style.width = '100px';
    button.innerHTML = 'Write';
    button.value = 'Write';
    button.name = 'Write';

document.getElementById("write_testi").appendChild(button);

See jsFiddle.

Comments

0

The other answers suggest switching to <input> (different Node) or using innerHTML (which deviates from your pure use of DOM methods so far).

You can do this in pure DOM methods as follows.

button.appendChild(document.createTextNode('Write'));

Comments

0

Try this:

function insertButton() {
    var button = document.createElement('input');
    button.type = 'button';
    button.setAttribute('name','write');
    button.setAttribute('class','ui-button_S40button');  
    button.setAttribute('style','width: 100px;');
    button.setAttribute('value','Write');  
    document.getElementById("write_testi").appendChild(button);
}

window.onload = insertButton;

Make sure write_testi is a div

If you have an existing function you call on window.onload, put code inside insertButton function in there.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.