8

I am trying to get a value fro query string and assign that value into a textbox. I am able to get the value from query string but unable to assign it to textbox.

document.getElementByName('Contact0Email').Value = email;

Tried the above code but doesn't seem to be working. Though the alert of email gives the right value.

Blank
  • 11
  • 2
  • 14
Prady
  • 10,978
  • 39
  • 124
  • 176
  • 1
    If you're just beginning with javascript, I suggest you take a look at the f-a-n-t-a-s-t-i-c jQuery library - jquery.com. In jquery you'd do the following: `$("#Contact0Email").val(email);` – Yuval Karmi Dec 27 '10 at 21:12
  • 1
    Also, I think you'd have better luck with [getElementById](http://www.w3schools.com/jsref/met_doc_getelementbyid.asp) instead of [getElementsByName](http://www.w3schools.com/jsref/met_doc_getelementsbyname.asp) (note the 's' in Elements) – Brad Christie Dec 27 '10 at 21:13
  • @yuval: I'd argue that it's better to get an appreciation of the DOM API first. Then use one of the many libraries available if you want. Knowing the DOM API helps prevent library abuse. – user113716 Dec 27 '10 at 21:19
  • 1
    @yuval, you're not going to load a 26 KB library just to save 26 characters. – ZippyV Dec 27 '10 at 21:21

1 Answers1

25

You need a lower-case value and a plural Elements:

document.getElementsByName('Contact0Email')[0].value = email;

You need the [0] to get the first element in the list. Names don't have to be unique like ids.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318