Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

issue with small juery code

Posted on 2012-03-29
8
Medium Priority
?
305 Views
Last Modified: 2012-03-29
$(document).ready(function () {
            $("#roleID").live("change",function() {
                  var id = $('select.foo').val();
                  var msg = "You are not allowed";
                  if (id != '5' || id != '24') {
                        alert(msg);
                        $('select.foo').val('5');
                  }
            });
      });


issue when my option is other than 5 & 24, it works fine and select vale comes to again to 5 after an alert message, now when i choose, it again shows me an alert even if the chosen value if 5. it happens with 24 value too.

guide
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37781288
rather than
var id = $('select.foo').val();

why not
var id = $(this).val();

and this
 $('select.foo').val('5');

with
 $(this).val('5');
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 2000 total points
ID: 37781322
I think you need to change the if clause like this

 if (id == '5' || id == '24') {
                     // Do nothing  
                  }
                  else
                  {
                    alert(msg);
                    $('select.foo').val('5');
                  }
0
 
LVL 35

Expert Comment

by:YZlat
ID: 37781463
try

  if (id != '5' && id != '24')

instead of

  if (id != '5' || id != '24')
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:YZlat
ID: 37781467
I don't know why but it took out the spaces:

if (id != '5'  &&  id != '24')
0
 
LVL 35

Expert Comment

by:YZlat
ID: 37781481
also try

if (parseInt(id)  !=  5  &&  parseInt(id)  !=  24)

Open in new window

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37781513
or may be because
if (id != '5' || id != '24') {

means that if id is not equal to 5 or not equal to 24. which means that even if it is equal to 5, it won't be equal to 24, so it will always fail

make it
if (id != '5' && id != '24') {
0
 
LVL 35

Expert Comment

by:YZlat
ID: 37782060
gurvinder372, I mentioned that above already
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 37782273
Thanks Guys, will try it
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question