devforall
devforall

Reputation: 7335

Why is my element value not getting changed? Am I using the wrong function?

I have an asp.net mvc application and i am trying to assign value to my textbox dynamically, but it seems to be not working (I am only testing on IE right now). This is what I have right now..

document.getElementsByName('Tue').Value = tue; (by the way tue is a variable)

I have also tried this variation but it didnt work either.

document.getElementsById('Tue').Value = tue; (by the way tue is a variable)

Can someone where please tell me where I am going wrong with this?

Upvotes: 22

Views: 215511

Answers (7)

Chintan Thummar
Chintan Thummar

Reputation: 1552

You can use

formname.textboxname.value="delete";

Upvotes: 4

KnightScott
KnightScott

Reputation: 123

If you are using Chrome, then debug with the console. Press SHIFT+CTRL+j to get the console on screen.

Trust me, it helps a lot.

Upvotes: 5

Kalaiyarasan
Kalaiyarasan

Reputation: 13464

There are two issues in your code.

  1. Use getElementByName instead of getElement**s**ByName
  2. use the value in lowercase instead of Value.

Upvotes: 5

Gumbo
Gumbo

Reputation: 655825

As the plural in getElementsByName() implies, does it always return list of elements that have this name. So when you have an input element with that name:

<input type="text" name="Tue">

And it is the first one with that name, you have to use document.getElementsByName('Tue')[0] to get the first element of the list of elements with this name.

Beside that are properties case sensitive and the correct spelling of the value property is .value.

Upvotes: 4

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67247

How to address your textbox depends on the HTML-code:

<!-- 1 --><input type="textbox" id="Tue" />
<!-- 2 --><input type="textbox" name="Tue" />

If you use the 'id' attribute:

var textbox = document.getElementById('Tue');

for 'name':

var textbox = document.getElementsByName('Tue')[0]

(Note that getElementsByName() returns all elements with the name as array, therefore we use [0] to access the first one)

Then, use the 'value' attribute:

textbox.value = 'Foobar';

Upvotes: 23

Paul Dixon
Paul Dixon

Reputation: 301135

It's document.getElementById, not document.getElementsByID

I'm assuming you have <input id="Tue" ...> somewhere in your markup.

Upvotes: 18

Jeff Handley
Jeff Handley

Reputation: 4164

Sounds like we need to assume that your textbox name and ID are both set to "Tue." If that's the case, try using a lower-case V on .value.

Upvotes: 4

Related Questions