Solved

issue with small juery code

Posted on 2012-03-29
8
294 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
Question by:myselfrandhawa
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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:myselfrandhawa
ID: 37782273
Thanks Guys, will try it
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now