jQuery syntax using $() function

$() function

  • jQuery uses a built-in function called jQuery() to allow developers to programmatically select elements on a html page

  • $() is shorthand for jQuery()

    • This shorthand syntax is used almost exclusively (because results in less typing) and that is what we will be using throughout the course

// programmatically "select" all p tags on the page using jQuery() syntax
jQuery('p')

// programmatically "select" all p tags on the page using the $() shorthand syntax; most widely used approach
$('p')


Example


  // Use jQuery to "select" an element with an id of main and listen for a click event
  $("#main").click(doSomething)