Javascript Cheatsheet

Check if Element With ID Exists

With jquery...

if($("#" + name).length == 0) {  
  //it doesn't exist
}

Get Whether Checkbox Ticked

Use jquery .is(':checked')

Execute Script When Document Loaded

$(document).ready()

You can chain these multiple times and all will execute. E.g.

$(document).ready(function(){
    doSomething(); 
})

$(document).ready(function(){
    doSomething2(); 
})

Using pure javascript, you can use:

body.onload(doSomething());  

This will execute when all of the body is loaded, but these cannot be "chained". You are setting the onload property of the body instead of adding more methods to a queue to be executed.

References

Author

Programster

Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus