Introduction and basics: advantage of jQuery and syntax; jQuery Selectors; jQuery Events (ready(), click(), keypress(), focus(), blur(), change())

jQuery એ write-less-do-more JavaScript છે: એક CDN file, $(selector).action() pattern, CSS-style selectors, અને 6 everyday events ready થી change સુધી.

12 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ચાર lines એક બને છે

BCA305-01 માં તમે FestConnect નું schedule panel ને honest way થી hide કર્યું:

document.getElementById("schedule").style.display = "none";

એક element, એક property, ceremony ની એક line. હવે committee search highlighting, dropdown reactions, 20 cards પર click handlers માંગે છે...

jQuery માં same job:

$("#schedule").hide();

નીચે same DOM, typing નો fraction. jQuery નું slogan એ તેનો entire sales pitch છે: write less, do more.

Theory

jQuery શું છે, અને શા માટે teams એ તેને adopt કર્યું

jQuery એ JavaScript library છે: pre-written functions ની એક .js file, તમારા script પહેલાં loaded, usually CDN થી:

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

Advantages જે exam lists કરે છે:

  • write less, do more: one-line DOM work
  • cross-browser: jQuery browser differences ને તમારી નીચે iron out કરે છે
  • DOM manipulation, events, effects અને AJAX ને simplify કરે છે (Units 2 અને 4 અહીં live કરે છે)
  • chaining: એક selection પર several actions ને string કરો
  • huge ecosystem, gentle learning curve

Formula

unit ને rule કરતી એક syntax

$(selector).action();

  • $ jQuery માં enters કરે છે
  • (selector) elements ને picks કરે છે, CSS-style
  • .action() દરેક element પર something કરે છે જે picked

અને તેની આસપાસ નો standard wrapper:

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

અંદરનું બધું ચાલે છે ફક્ત ત્યારે જ્યારે page નો DOM fully built થાય છે. આ unit નો દરેક jQuery example આ shell ને assume કરે છે.

At a glance

Selectors: CSS knowledge, reused

SelectorPicksFestConnect use
$("p")દરેક <p> elementબધા description paragraphs
$("#regBtn")એક element જેની તે id છેregister button
$(".eventCard")દરેક element જેની તે class છેબધા 20 event cards
$("*")page પર બધુંRare: bulk resets
$("p:first"), $("[href]")First <p>; કંઈ પણ જેની href છેRefinements

Practical

FestConnect wake up થાય છે (a complete page)

<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $(document).ready(function() {         // wait for the DOM
      $("#regBtn").click(function() {
        alert("Registration opens soon!");
      });
      $("#search").focus(function() {      // entering the field
        $(this).css("background-color", "#fff7e6");
      });
      $("#search").blur(function() {       // leaving the field
        $(this).css("background-color", "white");
      });
      $("#dept").change(function() {       // selection changed
        alert("Department: " + $(this).val());
      });
    });
  </script>
</head>
<body>
  <input id="search" placeholder="Search events">
  <select id="dept"><option>BCA</option><option>BCom</option></select>
  <button id="regBtn">Register</button>
</body>
</html>

This example runs in Gri-Learn on the web, where you can edit it and see the output.

At a glance

6 syllabus events

EventFires when
readyDOM fully parsed થાય છે (page structure exists)
clickelement clicked થાય છે
keypresscharacter key pressed થાય છે while typing
focusfield entered થાય છે (cursor arrive થાય છે)
blurfield left થાય છે (cursor depart થાય છે)
changevalue changed: selects instantly, text inputs on leaving after an edit

Quiz

$(".eventCard") શું select કરે છે?

  1. એક element જેની id eventCard છે
  2. દરેક element જેની class attribute માં eventCard છે
  3. page પર દરેક <eventCard> tag
  4. Nothing: jQuery selectors dots ને use નથી કરી શકતા
Show the answer

દરેક element જેની class attribute માં eventCard છે

dot prefix એ CSS ની class notation છે, અને jQuery એ CSS selectors ને wholesale adopt કર્યા: બધા 20 fest cards એક સાથે picked થાય છે, અને કોઈ પણ .action() દરેક પર apply થાય છે: તે all-at-once behaviour એ write-less-do-more નો અડધો ભાગ છે. Option A એ $("#eventCard") ને describe કરે છે, hash/id form: dot-vs-hash mix-up એ આ unit નો most common bug છે. Option C એ bare $("eventCard") હશે, tag selector જે tag ને hunt કરે છે જે exist નથી કરતું. તમારું BCA305-01 CSS knowledge already આ answer ને જાણતું હતું.

Think first

શા માટે બધું ready માં wrap કરવું?

script <head> માં sits કરે છે, body ની ઉપર. tap કરતા પહેલાં: શું $("#regBtn").click(...) કરે જો તે immediately run થાય, ready wrapper વગર, અને શા માટે?

Show the answer

તે run થાય તે પહેલાં browser એ body ને parse કર્યો નથી હોતો, તેથી #regBtn exist નથી કરતો yet: selector કંઈ MATCH નથી કરતું, handler કંઈ attach નથી કરતું, અને button dead રહે છે: કોઈ error નહીં, ફક્ત silence, જે maddening bug બનાવે છે. $(document).ready તમારા code ને defer કરે છે જ્યાં સુધી DOM complete ન થાય, તેથી દરેક element findable છે. આ same script-timing lesson છે જે BCA305-01 એ scripts-at-the-end-of-body સાથે શીખવ્યું; ready એ jQuery નું cleaner spelling છે. shorthand $(function(){...}) same job કરે છે.

Watch out

ત્રણ first-week jQuery bugs

Missing prefix: $("regBtn") એ TAG selector છે; id ને $("#regBtn") ની જરૂર છે, class ને dot ની જરૂર છે. Silent no-match, dead handler.

CDN line ને Forgetting કરવું: jQuery ને load કરતા script tag વગર first, $ undefined છે અને console તે કહે છે; library તમારા code પહેલાં load થવી જોઈએ.

blur vs change confusion: blur દરેક exit પર fires થાય છે field માંથી; change ફક્ત જ્યારે value actually changed થાય છે. Validation usually blur ને wants છે; reaction-to-edits change ને wants છે.

Theory

તે હજુ તમારો DOM છે underneath

jQuery ને new language તરીકે file ન કરો: $("#regBtn").click(...) exact getElementById અને addEventListener machinery ને wrap કરે છે જે તમે BCA305-01 માં hand-wrote કર્યું; loaded library પર view-source કરો અને તે JavaScript છે all the way down. તે mental model તમને debuggable રાખે છે: જ્યારે jQuery તમને surprise કરે, DOM ને reason કરો underneath. next lesson fest page move થવાનું શરૂ કરે છે: show, hide, fade અને slide, plus chaining જે advantages list promise કર્યું.

Summary

Key takeaways

  • jQuery = JavaScript library, એક CDN file તરીકે loaded તમારા script પહેલાં.
  • Advantages: write less do more, cross-browser, easy DOM/events/effects/AJAX, chaining.
  • Core syntax: $(selector).action(); code ને $(document).ready(function(){...}) માં wrap કરો.
  • Selectors એ CSS છે: tag $("p"), id $("#x"), class $(".y"), everything $("*").
  • છ events: ready, click, keypress, focus, blur, change; $(this) એ firing element છે.
  • Class selectors બધા matches પર act કરે છે એક સાથે: 20 cards, એક line.
  • Memory hook: dollar picks, dot acts, ready waits.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from jQuery Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Introduction and basics: advantage of jQuery and syntax; jQuery Selectors; jQuery Events (ready(), click(), keypress(), focus(), blur(), change()) · Web Designing-2 (option A) · Gri-Learn