Julian Vickers
Julian Vickers

Reputation: 33

Cannot change value of dynamically created textbox

I have been trying various means of doing this all day, and nothing has done the job yet. What I have is a text box that is part of a dynamically generated HTML table... users can add rows or delete them. In this instance I would like for users to be able to enter a value into the text box, and when the focus changes from the box to another part of the table I would like it to save the changed value in the place of the default value. No matter what I try the value always remains blank. Here's the dynamically generated table element:

function ...() {

    var addText = document.createElement('input');
            addText.setAttribute('type','text');
            addText.setAttribute('name','comment');
            addText.setAttribute('id','text');
            addText.setAttribute('value','');
            addText.setAttribute('onblur','changeText(this)');

...
}

and the function changeText():

function changeText () {
    var comment = getElementById('text').value;
    /* How to get it back? */
}

I can see that the problem (I think) is that I am not getting comment back to correct spot in the calling function but the solution is eluding me!

Upvotes: 0

Views: 640

Answers (2)

shubniggurath
shubniggurath

Reputation: 966

You're setting the same id multiple times. Id is a unique identifier. You want class. That's part of your problem. Also your function isn't receiving 'this'. So that's a problem.

Upvotes: 1

Praveen
Praveen

Reputation: 56509

You have to change this line

var comment = getElementById('text').value;

to

 var comment = document.getElementById('text').value;

Upvotes: 1

Related Questions