1
$(document).ready(function() {
  function validate() {
    var d1 = document.getElementById("edit-submitted-23");
    var d2 = document.getElementById("edit-submitted-24");

    if (d1 == d2) {
      alert("You have choosen the same value");
    }
  }
});

I am trying to check if selected values are same in different dropdowns with Javascript. However the code that I use gives no alert.

4 Answers 4

2

You are checking the elements directly, where you want to check values.

So your condition should be

 if (d1.value == d2.value) {

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

4 Comments

typo from original post: dd2 -> d2
@FabianN. Good catch.
I have changed my code but still I dont get any alert on the screen
@allstar Where you are calling your validate ?
1

Compare the values:

if (d1.value === d2.value) {
//... your code here
}

1 Comment

typo from original post: dd2 -> d2
1

What your code does is compare two DOM references together. This will always give false as you are comparing different HTML elements. What you mean to do is to compare the elements’ values. Your code should be like this:

$( document ).ready(function() {
     var d1 = document.getElementById("edit-submitted-23");
     var d2 = document.getElementById("edit-submitted-24");

     if (d1.value == d2.value)
     { 
       alert("You have choosen the same value");
     }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="edit-submitted-23">
  <option>1</option>
  <option>2</option>
  <option>3</option>  
</select>

<select id="edit-submitted-24">
  <option>1</option>
  <option>2</option>
  <option>3</option>  
</select>

I should also note that your code defines a function validate() but doesn’t use it. You don’t need to define it. Or if you do, you need to call it afterwards:

$(document).ready(function(){
   function validate(){
       ....
   }

   validate();
})

7 Comments

I have tried this one but still ı dont get any alert on the screen
Could you log d1.value and d2.value? (Use console.log())
Hi sorry, I forgot to mention that you need to call validate(). Check the updates to my answer.
Thanks for your help.Now my code works but not dynamics it gives the alert just when I reload the page collabedit.com/anrcj
Your code runs only when the page loads. If you wish for it to run whenever a menu is updates, you should define your validate() method outside of $(document).ready(). Then give both <select> elements onchange like this: <select onchange=validate()>.
|
0

Since you are using JQuery, you can do check like this:

function validate() {
  if($("#edit-submitted-23").val() === $("#edit-submitted-24").val()) {
    alert('You have choosen the same value');
  }
}

Assuming you are calling validate() on some event.

5 Comments

Thanks for your help.Now my code works but not dynamics it gives the alert just when I reload the page collabedit.com/anrcj
It will give alert() when you call this function and not when page reloads. When are you calling this function???
I am calling it on button click collabedit.com/26svw I dont have to do ıt dynamıc actually ıf ıt gıves the alert after button click it s also ok however after I added button click part in the code it doesnt give alert
Are you still looking for answers? because you have already accepted another answer.
yes because after I have added button click,it doesnt give alert anymore

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.