jQuery and JavaScript for Debugging

Here are a few of the JavaScript and jQuery methods which I find useful when debugging my projects.

  • JSON.stringify() can be used to inspect objects
  • To inspect the value of element attributes like class or id, use .attr() e.g. $('#myelementid').attr('class')
  • For an element with multiple classes, you can check if it has a particular class using .hasClass()
  • $(selector).length gives the count of the matched elements

… and things to check if code isn’t working as expected

  • If jQuery code is unexpectedly failing to have any effect on an element in the document, go back to basics and make sure you have put the call to the code in the document‘s ready event so that it is not called until all the elements are present:
    $(document).ready(function(){
    // code
    });
Advertisements

About Jennifer Phillips Campbell

Software Developer and Medieval Historian
This entry was posted in JavaScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s