SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need a function that does not allow me to select same value from list menu

    Hi Guys,

    I need a function that will reset the form if the user selects the same value from both of the dropdowns below. How can I do that?

    HTML Code:
          <tr> 
            <td width="28%">Most like me:</td>
            <td width="72%"><select name="most_like_me" size="4">
                <option value="1">Answer 1</option>
                <option value="2">Answer 2</option>
                <option value="3">Answer 3</option>
                <option value="4">Answer 4</option>
              </select></td>
          </tr>
          <tr> 
            <td>Least like me:</td>
            <td><select name="least_like_me" size="4">
                <option value="1">Answer 1</option>
                <option value="2">Answer 2</option>
                <option value="3">Answer 3</option>
                <option value="4">Answer 4</option>
              </select></td>
          </tr>
    Thanks in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the function:
    Code JavaScript:
    function check(){
      var sel1 = document.getElementById('most_like_me');
      var sel2 = document.getElementById('least_like_me');
      var val1 = sel1.options[sel1.selectedIndex].value;
      var val2 = sel2.options[sel2.selectedIndex].value;
      if(val1 == val2){
        alert('Answers can not be same!');
        return;
      }
      else
        alert('OK');
    }
    Suppose you have some button to trigger it like:
    Code HTML4Strict:
    <input type='button' value='Check' onclick='check()'>
    Also you will have to add an id's to select tags:
    Code HTML4Strict:
    <select name="most_like_me" id="most_like_me" size="4">
    <select name="least_like_me" id="least_like_me" size="4">

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that works! But can I get it so that it just resets both selections?

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I forgot that. You need form tag (in case you don't have it) i.e.
    Code HTML4Strict:
    <form id='myform'>
    <select...
    <option...
    ...
    </form>
    Wrap select tags with form tags and then add to function after alert('Answers can....:
    Code JavaScript:
    document.getElementById('myform').reset()

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much


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
  •