Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to add input box value?

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

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    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
    Code:
    <input id="sel4" type="button" value="Click me!"
     onclick="function(evt){
      // 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 01: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
    •