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 thedocument
‘sready
event so that it is not called until all the elements are present:
$(document).ready(function(){
// code
});