JavaScript jQuery if has() selector do stuff

<ul id="bad-drifting">
    <li>text</li>
</ul>

http://jsfiddle.net/wZ8MC/2/

jQuery(document).ready(function() { 
    if (jQuery('#bad-drifting').has('em')) {
        jQuery('#bad-drifting').css({'color': 'red'});
        jQuery('#bad-drifting').css({'font-weight': 'bold'});
    }
});

http://jsfiddle.net/Xzn6y/

jQuery(document).ready(function() { 
    if (jQuery('#bad-drifting li:has(em)')) { // .has('em')
        jQuery('#bad-drifting').css({'color': 'red'});
        jQuery('#bad-drifting').css({'font-weight': 'bold'});
    }
});

What I want to do is not just set some CSS but make more complicated changes but I can't figure out why this always returns true... Is it a bug or what am I missing?

Answer:1

You need to check for length because jQuery('#bad-drifting li:has(em)') returns a jQuery object which will be always truthy.

if (jQuery('#bad-drifting li:has(em)').length) { // .has('em')
Answer:2
if (jQuery('#bad-drifting').has('em')[0]) { // do Stuff
Answer:3

I ended up doing like this:

if (jQuery('#bad-drifting em').length > 0) {
    console.log('we have some errors');
}   
Answer:4

I am merely looking for some help as a newbie learning webdesign, please understand I am still in the very early stages of learning when answering my question. I have the following: <script src="...

I am merely looking for some help as a newbie learning webdesign, please understand I am still in the very early stages of learning when answering my question. I have the following: <script src="...

  1. javascript jquery image carousel
  2. jquery script for zooming image on mouseover
  3. javascript jquery image

I am fairly new to using SugarCRM and I am just trying to do some validation on a form page I have. I am simply trying to get the value of my dropdown, and perform my error if it is equal to "None" ...

I am fairly new to using SugarCRM and I am just trying to do some validation on a form page I have. I am simply trying to get the value of my dropdown, and perform my error if it is equal to "None" ...

  1. variable dropdown selected value

How I have a basic javascript registration form which sends the password that an user has filled to a secure axis2 web service (which stores the salted hash of the password in a database). Mu ...

How I have a basic javascript registration form which sends the password that an user has filled to a secure axis2 web service (which stores the salted hash of the password in a database). Mu ...

  1. secure client javascript
  2. javascript secure websocket client example
  3. javascript secure websocket client

I am building an windows app using Tide SDK. How can I open a local file from my app. For example, I have a pdf file somewhere in my harddisk. I put the link to that file to my app. When I click on ...

I am building an windows app using Tide SDK. How can I open a local file from my app. For example, I have a pdf file somewhere in my harddisk. I put the link to that file to my app. When I click on ...

  1. open local file in chrome
  2. open local file javascript
  3. open local file python
  4. open local file in browser
  5. open local file in google docs
  6. open local file in safari
  7. open local file in firefox
  8. open local file in edge
  9. open local file html
  10. open local file links in chrome
  11. open local file from browser link
  12. open local file in jupyter notebook
  13. open local file revit
  14. open local file c#
  15. open local file in office 365
  16. open local file with excel online
  17. open local file in chrome android
  18. open local file with barcode
  19. open local file with remote app
  20. open local file javascript chrome