SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Real Basic - Change Form Name and Value

    Hi there,

    I've got a really basic question - I just don't know a whole lot about client side javascript.

    I need to change the name and value of a hidden form field before it gets submitted.

    Here's an example:

    HTML Code:
    <select name='dropdown'>
      <option name = 'option1'>Option Number 1</option>
      <option name = 'option2'>Option Number 2</option>
    </select>
    <input name='textbox' type='text' />
    Then I have this

    HTML Code:
    <input type='hidden' name='XXXXX' value='YYYYY'>
    where 'XXXXX' is the value selected from the drop down, and 'YYYYY' is the value of the textbox.

    Not sure if this is basic or not, but I was wondering if someone could give me a pointer. Thanks!

  2. #2
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if you have to do this (because your server script should be able to cope with a drop down and a text field and then if your users don't have js thay aren't messed up) you could do something like this.

    Firstly slap an id on all three (the selectm the text box and the hidden I'll call them select textbox and hidden but knock yourself out). Theen attach a function much like this one to the forms onsubmit event handler.

    Code:
    function update_field()
    {
       var target =  document.getElementById('hidden');
       var select = document.getElementById('select'); 
       target.name = select.options[select.selectedIndex].value;
       target.value = document.getElementById('textbox');
    }
    But your best plan is to deal with it server side. IMHO.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks,

    That helped. Basically it needs to be client side because it has to happen before the post. Since it's an intranet, we can mandate that the client has javascript turned on.


Bookmarks

Posting Permissions

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