0

Hey guys I am using JavaScript to select a specific value(option) from the html select form tag, but whenever I call my JavaScript function I get a single message repeating for all the choices I want to select. Ex: If I choose to select 'Rabbit' from the list of options and then display a message saying 'Rabbit chosen'; the message will display for each option/value selected.

Here is my JavaScript Code:

    var element = document.getElementById('choices').value;

    function SelectElement() {    
      if (element = 'Rabbit') {
          alert("Rabbit Selected");
      }
      else if (element = 'Wall') {
          alert("Wall Selected");
      }
      else if (element = 'Arrow') {
          alert("Arrow Selected");
      }      
   } 

Here is my html code:

 <form>
     <select id="choices" >
        <option>Rabbit</option>
        <option>Wall</option>
        <option>Arrow</option>
     </select>

     <input type="button" value="Submit" onclick="SelectElement()"/>
 </form>

Can you smart guys please help me????

4
  • You have made mistake in your if statement, Do it double equal and it will fix your issue. Commented Jan 1, 2016 at 12:31
  • hmm.. didn't work for me. Commented Jan 1, 2016 at 21:56
  • I used the comparison operator, but nothing happens when I click submit. Commented Jan 1, 2016 at 22:01
  • can you take that var element inside the function? Commented Jan 2, 2016 at 5:15

3 Answers 3

4

A. You should fetch the value each time before calling the function and then check it otherwise your element variable won't refresh at all.

B. To compare two values you should use ==, = is an assignment operator.

   function SelectElement() { 
      var element = document.getElementById('choices').value;   
      if (element == 'Rabbit') {
          alert("Rabbit Selected");
      }  
   } 

(As your question is not much clear) if you just want to alert the selected value for every option clicked, just do:

  function SelectElement() { 
      var element = document.getElementById('choices').value;   
      alert(element+" Selected");
   } 

This is basic string concatenation.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I missed everything after A. I removed it.
@heisenberg it okay :)
nice one @void but how do I fetch the value each time before calling the function so that my variable element refreshes?
I used the comparison operator, but nothing happens when I click submit.
0

There is a something called selected == true or false in a "select" tag.
You could write in HTML :

<form>
  <select id="choices">
    <option id="Rabbit">Rabbit</option>
    <option id="Wall">Wall</option>
    <option id="Arrow">Arrow</option>
  </select>
</form>
<button onclick="TakeElement()">Click</button>

You could write in javascript:

   var ra = document.getElementById('Rabbit');
    var wa = document.getElementById('Wall');  
var ar = document.getElementById('Arrow');

   function TakeElement() {
      if (ra.selected == true) {
        alert("Rabbit is selected");
      }
      if (wa.selected == true) {
        alert("Wall is selected");
      }
      if (ar.selected == true) {
        alert("Arrow is selected");
      }
   }

2 Comments

Your answer adds nothing over the existing answer, which was posted about 45 minutes before yours.
Yes it does, nobody use the .selected == true or false in all thiese answers
-2

I think you must replace element ='rabbit' with element =='rabbit'

== is comparison operator and = is assignment operator

4 Comments

You haven't added a single thing to your answer of what wasn't in any of the others. You are fishing for reputation
Why down-votes? Isn't my answer correct? Seriously I think this website is not as good as I thought. Btw, the answer for simple questions must be concise, not like a big essay questions like you people who fish for reputations do. Go and learn some skills before pointing others. +SuperDJ
Not my downvote, but if the problem is entirely caused by a typo, the question should be closed (since typos almost never help future users). Note that the incorrect operator is not the only problem with the code here.
Clearly it's not a typo, he is just confused between assignment and comparison operator it seems and hence I gave him the simplest of the answers without unnecessary stuff as I wasn't fishing for reputation. Kindly check before downvoting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.