View Full Version : How to add input box value?

04-15-2009, 01:48 AM
Got it working

04-15-2009, 02:47 AM
Welcome to the forums.

I'll prefix this by saying I can't stand inline handlers, but if that's the way you want to go, "ok". It's cases like this where the more you want to do, the harder it is to implement inside an element tag.

You need to grab the value from the "sel1" id field and concatenate it onto the end of the URL, something like this

<input id="sel4" type="button" value="Click me!"
// grab the value from the other field
var inputValue= document.getElementById('sel1').value;
// call your 'createPlayer' function
createPlayer('http://gdata.youtube.com/feeds/api/videos?q=' +encodeURIComponent( inputValue ) );
return false;

Things to note:
Formatting is to illustrate how the event handler works, you can slap this all on one line if it works for you.
I'm wrapping the event handler code inside an anonymous function which takes the event object as an argument for use inside the function call.
Use of getElementById to retrieve the element named 'sel1' and then reference the value in it.
Use of the concatenation operator outside the 'quotes' to tack on the variable 'inputValue' we've just grabbed from the other field.
Use of 'return false' to keep the input element from submitting a form or some other action. You could also use the event object's preventDefault method, e.g. evt.preventDefault(). I think in this specific case this is optional, but good practice, especially when using forms and anchor tags.

edit: use of encodeURIComponent for good measure. YMMV

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum