Solved

issue with small juery code

Posted on 2012-03-29
8
296 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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: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 16

Author Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

809 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