?
Solved

issue with small juery code

Posted on 2012-03-29
8
Medium Priority
?
302 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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, …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

752 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