Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Javascript GetElementById of a textbox

hi i have been trying to get the value entered in a text box by running this function but with no luck , where am i going wrong please?

var textbox = document.getElementsByName('paul').value;
function myFunction(){

    alert('you typed - '  + textbox);

}

I run this function in html through the onClick method, i appreciate the help all i get back is undefined every time :(

1 Answer

Hi Paul,

The code you posted uses getElementsByName which returns an array of elements (even if there is just one element that matches).

You either need to select the first element in the array...

var textbox = document.getElementsByName('paul')[0].value;

... or you could just use getElementById

var textbox = document.getElementsById('paul').value;

I'd recommend using the later.

Hope this helps :)