jquery

The purpose of jQuery is to make it much easier to use Javascript on your website.

   1 wget https://code.jquery.com/jquery-3.7.1.min.js

DOM ready

   1 $(document).ready(function(){
   2 //......
   3 });
   4 //-----------
   5 $(document).ready(readyx);
   6 function readyx(){
   7 // called on DOM ready
   8 }

Find element by id

   1 // element id id1234
   2 var x1234 = $("#id1234");

Find elements by class

   1 // element with class classsdf
   2 var elements = $(".classsdf");
   3 elements.each(  eachHandler  );
   4 
   5 function eachHandler(index,element){
   6 console.log(index + ' ' +element);
   7 }

Get or set select dropdownlist selected value

   1 $("#dropdown").val(); //get value
   2 $("#dropdown").val('asd'); //select value asd
   3 

.attr( attributeName )

Returns String Get the value of an attribute for the first element in the set of matched elements.

.text()

Returns String Get the combined text contents of each element in the set of matched elements, including their descendants.

html()

Returns String Get the HTML contents of the first element in the set of matched elements.

.append( content [, content ] )

Returns jQuery Insert content, specified by the parameter, to the end of each element in the set of matched elements.

.empty()

Returns jQuery Remove all child nodes of the set of matched elements from the DOM.

jQuery.get( url [, data ] [, success ] [, dataType ] )

Returns jqXHR Load data from the server using a HTTP GET request.

   1 $.get( "test.cgi", { name: "John", time: "2pm" } )
   2   .done(  function( data ) {
   3     alert( "Data Loaded: " + data );
   4   });
   5 
   6 $.ajax({
   7   url: url,
   8   data: data,
   9   success: success, // function(data, textStatus,jqXHR )
  10   error: error , // function( jqXHR, textStatus, errorThrown )
  11   dataType: dataType // text, html, xml, json, jsonp, and script.
  12 });

Select element with . and : in id

https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

   1 function jq( myid ) {
   2     return "#" + myid.replace( /(:|\.|\[|\])/g, "\\$1" );
   3 }

Ajax error handler

   1 function onError(xhr, status, error){
   2     console.log(xhr.responseText); // text returned on error
   3 }

Javascript/jquery (last edited 2025-02-08 14:02:02 by vitor)