Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Jquery not hasClass

Posted on 2012-08-21
7
Medium Priority
?
1,387 Views
Last Modified: 2012-08-22
Hi

I'm having a spot of bother with the following selection whilst looping through tables.

the "Do Stuff" code is   being executed on tables with  class ' BadClass'

What am i doing wrong? (please ignor typos)

Is there a better way of doing this selection?

$("table:not(.borderless)").each(function(){
  if(($(this).attr('id') != "badID") ||  (!$(this).hasClass('BadClass')))
   {
   // Do stuff
  }
});

Open in new window

0
Comment
Question by:trevor1940
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Assisted Solution

by:teebon
teebon earned 1000 total points
ID: 38315271
Hi trevor,

Please try to change the OR to AND at this line :

if(($(this).attr('id') != "badID") &&  (!$(this).hasClass('BadClass')))

Open in new window

0
 
LVL 13

Accepted Solution

by:
JonNorman earned 1000 total points
ID: 38315288
with the or (||) it would execute on the items unless they had an id of "badID" AND a class of 'BadClass, you need an AND (&&). The code should be:
$("table:not(.borderless)").each(function(){
  if($(this).attr('id') != "badID" &&  !$(this).hasClass('BadClass')){
   // Do stuff
  }
});

Open in new window

0
 
LVL 1

Author Comment

by:trevor1940
ID: 38315594
Hi

sorry for confusion but

BadID and BadClass are not the same table so changing '||' to '&&' isn't going to work.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:teebon
ID: 38315604
Have you tried changing?
0
 
LVL 13

Expert Comment

by:JonNorman
ID: 38315656
BadClass Table; isBadClass = true; isBadID = false:
if($(this).attr('id') != "badID" ||  !$(this).hasClass('BadClass')) = (true or false) = true therefore do Something

BadID Table; isBadClass = false; isBadID = true:
if($(this).attr('id') != "badID" ||  !$(this).hasClass('BadClass')) = (false or true) = true therefore do Something

Therefore change it to an && as
true && false = false
true || false = true
0
 
LVL 1

Author Comment

by:trevor1940
ID: 38315729
OK Brain Fry && did work I've no idea why?

I thought && ment both conditions needed to be true eg  the equivelant of

if ( A is true ) AND (B is True)
 do stuff

to my mind what your both suggesting is  the equivelant of

if ( A is true ) OR B is True)
 do stuff
0
 
LVL 13

Expert Comment

by:JonNorman
ID: 38316124
You have got it the wrong way round!

Either condition can be true if you OR them together, both conditions need to be true if you AND them together.

We are saying that you want either one to be false for it not do the code.

Therefore you need to check both and if either are false then don't do it.


Therefore it is an AND that is needed.

true AND false = false
false AND true = false
true OR false = true
false OR true = true

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

What you had said is if this or that then do it! the trouble was with what you have is that this or that is always true.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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

580 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