Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Thanked 0 Times in 0 Posts

    How to add input box value?

    Got it working
    Last edited by troc52; 04-15-2009 at 03:08 AM. Reason: Got it working

  • #2
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Camarillo, CA US
    Thanked 83 Times in 82 Posts
    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
    Last edited by bdl; 04-15-2009 at 02:54 AM. Reason: Added encodeURIComponent


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts