Solved

issue with small juery code

Posted on 2012-03-29
8
295 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
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
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 500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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:gurvinder372
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 15

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now