Categories
jQuery

jQuery — Mouse Events and Multiple Selections

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.

.mousemove()

The .mousemove() method lets us add an event listener for the mousemove event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mousemove(function() {
  console.log("Handler for .mouseleave() called.");
});

Also, we can trigger then mousedown event on the div with ID target when we click on the div with ID other by writing:

$("#target").mousemove(function() {
  console.log("Handler for .mouseleave() called.");
});

$("#other").click(function() {
  $("#target").mousemove();
});

.mouseout()

The .mouseout() method lets us add an event listener for the mouseout event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mouseout(function() {
  console.log("Handler for .mouseout() called.");
});

Also, we can trigger then mousedown event on the div with ID target when we click on the div with ID other by writing:

$("#target").mouseout(function() {
  console.log("Handler for .mouseleave() called.");
});

$("#other").click(function() {
  $("#target").mouseout();
});

.mouseover()

The .mouseover() method lets us add an event listener for the mouseover event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mouseover(function() {
  console.log("Handler for .mouseover() called.");
});

Also, we can trigger then mousedown event on the div with ID target when we click on the div with ID other by writing:

$("#target").mouseover(function() {
  console.log("Handler for .mouseover() called.");
});

$("#other").click(function() {
  $("#target").mouseover();
});

.mouseup()

The .mouseup() method lets us add an event listener for the mouseup event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mouseup(function() {
  console.log("Handler for .mouseup() called.");
});

Also, we can trigger then mouseup event on the div with ID target when we click on the div with ID other by writing:

$("#target").mouseup(function() {
  console.log("Handler for .mouseup() called.");
});

$("#other").click(function() {
  $("#target").mouseup();
});

Multiple Attribute Selector [name=”value”][name2=”value2″]

We can select items with multiple attributes with the multiple attribute selector.

For example, if we have:

<input id="man-news" name="man-news">
<input name="milkman">
<input id="letterman" name="new-letterman">
<input name="newmilk">

Then we can get the input with ID letterman and name value new-letterman by writing:

$("input[id][name$='man']").val("only this one");

After we got the element, we call val to set the input value.

Multiple Selector (“selector1, selector2, selectorN”)

The multiple selector selector lets us get elements that match any of the given selectors.

For example, if we have:

<div>div</div>
<p class="myClass">p class="myClass"</p>
<p class="notMyClass">p class="notMyClass"</p>
<span>span</span>

Then we can add a red border to the div , span , and the p element with class myClass by writing:

$("div, span, p.myClass").css("border", "3px solid red");

Conclusion

We can listen and trigger to mouse events and select elements with jQuery.

Categories
jQuery

jQuery — Mouse Events and Element Count

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.

.length

We can get the number of elements in the jQuery object with the .length property.

For example, if we have:

<div></div>

Then we add a div and get the total number of divs by writing:

$(document.body).append($("<div>"));
const n = $("div").length;
console.log(n);

The console log should log 2.

.load()

The .load() method lets us load data from the server and place the returned HTML into matched elements.

For example, if we have:

<div id='result'></div>

Then we can write:

$("#result").load("https://jsfiddle.net");

to load the content of https://jsfiddle.net into the div with ID result .

.map()

The .map() method lets us map the values of each item into a new value.

For example, if we have:

<form method="post" action="">
  <fieldset>
    <div>
      <label for="two">2</label>
      <input type="checkbox" value="2" id="two" name="number[]">
    </div>
    <div>
      <label for="four">4</label>
      <input type="checkbox" value="4" id="four" name="number[]">
    </div>
    <div>
      <label for="six">6</label>
      <input type="checkbox" value="6" id="six" name="number[]">
    </div>
    <div>
      <label for="eight">8</label>
      <input type="checkbox" value="8" id="eight" name="number[]">
    </div>
  </fieldset>
</form>

Then we get all the checkboxes’ IDs and then join them into a string by writing:

const str = $(":checkbox")
  .map(function() {
    return this.id;
  })
  .get()
  .join();
console.log(str);

Then str is ‘two,four,six,eight’ .

.mousedown()

The .mousedown() method lets us add an event listener to the mousedown event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mousedown(function() {
  console.log("Handler for .mousedown() called.");
});

Also, we can trigger then mousedown event on the div with ID target when we click on the div with ID other by writing:

$("#target").mousedown(function() {
  console.log("Handler for .mousedown() called.");
});

$("#other").click(function() {
  $("#target").mousedown();
});

.mouseenter()

The .mouseenter() method lets us add an event listener for the mousedown event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mouseenter(function() {
  console.log("Handler for .mouseenter() called.");
});

Also, we can trigger then mousedown event on the div with ID target when we click on the div with ID other by writing:

$("#target").mouseenter(function() {
  console.log("Handler for .mouseenter() called.");
});

$("#other").click(function() {
  $("#target").mouseenter();
});

.mouseleave()

The .mouseleave() method lets us add an event listener for the mouseleave event or trigger it.

For example, if we have:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to the mouswdown event on the div with ID target by writing:

$("#target").mouseleave(function() {
  console.log("Handler for .mouseleave() called.");
});

Also, we can trigger then mousedown event on the div with ID target when we click on the div with ID other by writing:

$("#target").mouseleave(function() {
  console.log("Handler for .mouseleave() called.");
});

$("#other").click(function() {
  $("#target").mouseleave();
});

Conclusion

We can listen or trigger to mouse events with jQuery.

Categories
jQuery

jQuery — Thenables and Keydown

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.

jQuery.ready

The jQuery.ready is a thenable object that resolves when the document is ready.

For example, we can write:

(async () => {
  const data = await $.when(
    $.getJSON("https://jsonplaceholder.typicode.com/posts/1"),
    $.ready
  )
  console.log(data);
})()

We call getJSON to get JSON data.

Then we call $.ready to wait for the document to be ready.

Then we get the data after both operations are done.

jQuery.readyException()

The jQuery.readyException method handles errors that are thrown synchronously in functions wrapped in jQuery.

For example, we can write:

jQuery.readyException = function(error) {
  console.error(error);
};

to pass the error object into the console.error method and log it.

jQuery.removeData()

The jQuery.removeData() method lets us remove data from an element.

For example, if we have:

<div></div>

Then we can use removeData to remove data from the div:

const div = $("div")[0];
jQuery.data(div, "test1", "value");
console.log(jQuery.data(div, "test1"))
jQuery.removeData(div, "test1");
console.log(jQuery.data(div, "test1"))

We get the div and then call jQuery.data to get the data.

Then we call removeData to remove the data.

So the first console log logs ‘value’ and the 2nd one logs undefined .

jQuery.support

The jQuery.support property has an object that shows what browser features are supported.

jQuery.uniqueSort()

The jQuery.uniqueSort() method sorts an array of DOM elements in place with the duplicates removed.

For example, if we have:

<div></div>
<div class="dup"></div>
<div class="dup"></div>
<div class="dup"></div>
<div></div>

Then we can use it as follows:

let divs = $("div").get();
divs = divs.concat($(".dup").get());
console.log(divs.length)
divs = jQuery.uniqueSort(divs);
console.log(divs.length)

We get the divs. Then we add the divs with class dup into the divs element list.

Then we call uniqueSort to sort them and remove the duplicates.

Therefore, the first console log logs 8 and the 2nd one logs 5.

jQuery.when()

We can use the jQuery.when() method to run callback functions based on 0 or more thenable objects.

For example, we can write:

(async () => {
  const data = await $.when($.ajax("https://jsonplaceholder.typicode.com/posts"))
  console.log(data);
})()

to make a GET request and then log the data.

.keydown()

The .keydown() method lets us listen to the keydown event on an element.

For example, if we have:

<form>
  <input id="target" type="text" value="Hello there">
</form>
<div id="other">
  Trigger the handler
</div>

Then we can use it to listen to the keydown events on the input element by writing:

$("#target").keydown(function() {
  console.log("Handler for .keydown() called.");
});

We can also use it to trigger a keydown event on another element.

For example, if we have the same HTML, then we can write:

$("#target").keydown(function() {
  console.log("Handler for .keydown() called.");
});

$("#other").click(function() {
  $("#target").keydown();
});

to trigger the keydown event on the input when we click on the div with ID other .

Conclusion

We can run thenable code and then wait for the result with jQuery.

Also, we can listen or trigger keydown events.

Categories
jQuery

jQuery — Thenables and Keydown

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.

jQuery.ready

The jQuery.ready is a thenable object that resolves when the document is ready.

For example, we can write:

(async () => {
  const data = await $.when(
    $.getJSON("https://jsonplaceholder.typicode.com/posts/1"),
    $.ready
  )
  console.log(data);
})()

We call getJSON to get JSON data.

Then we call $.ready to wait for the document to be ready.

Then we get the data after both operations are done.

jQuery.readyException()

The jQuery.readyException method handles errors that are thrown synchronously in functions wrapped in jQuery.

For example, we can write:

jQuery.readyException = function(error) {
  console.error(error);
};

to pass the error object into the console.error method and log it.

jQuery.removeData()

The jQuery.removeData() method lets us remove data from an element.

For example, if we have:

<div></div>

Then we can use removeData to remove data from the div:

const div = $("div")[0];
jQuery.data(div, "test1", "value");
console.log(jQuery.data(div, "test1"))
jQuery.removeData(div, "test1");
console.log(jQuery.data(div, "test1"))

We get the div and then call jQuery.data to get the data.

Then we call removeData to remove the data.

So the first console log logs ‘value’ and the 2nd one logs undefined .

jQuery.support

The jQuery.support property has an object that shows what browser features are supported.

jQuery.uniqueSort()

The jQuery.uniqueSort() method sorts an array of DOM elements in place with the duplicates removed.

For example, if we have:

<div></div>
<div class="dup"></div>
<div class="dup"></div>
<div class="dup"></div>
<div></div>

Then we can use it as follows:

let divs = $("div").get();
divs = divs.concat($(".dup").get());
console.log(divs.length)
divs = jQuery.uniqueSort(divs);
console.log(divs.length)

We get the divs. Then we add the divs with class dup into the divs element list.

Then we call uniqueSort to sort them and remove the duplicates.

Therefore, the first console log logs 8 and the 2nd one logs 5.

jQuery.when()

We can use the jQuery.when() method to run callback functions based on 0 or more thenable objects.

For example, we can write:

(async () => {
  const data = await $.when($.ajax("https://jsonplaceholder.typicode.com/posts"))
  console.log(data);
})()

to make a GET request and then log the data.

.keydown()

The .keydown() method lets us listen to the keydown event on an element.

For example, if we have:

<form>
  <input id="target" type="text" value="Hello there">
</form>
<div id="other">
  Trigger the handler
</div>

Then we can use it to listen to the keydown events on the input element by writing:

$("#target").keydown(function() {
  console.log("Handler for .keydown() called.");
});

We can also use it to trigger a keydown event on another element.

For example, if we have the same HTML, then we can write:

$("#target").keydown(function() {
  console.log("Handler for .keydown() called.");
});

$("#other").click(function() {
  $("#target").keydown();
});

to trigger the keydown event on the input when we click on the div with ID other .

Conclusion

We can run thenable code and then wait for the result with jQuery.

Also, we can listen or trigger keydown events.

Categories
jQuery

jQuery — Queues and Serialized Data

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.

jQuery.merge()

The jQuery.merge() method merges the contents of 2 arrays together into the first array.

For example, we can use it by writing:

const arr = $.merge([0, 1, 2], [2, 3, 4])
console.log(arr);

Then arr is:

[0, 1, 2, 2, 3, 4]

jQuery.noConflict()

The jQuery.noConflict() method relinquishes jQuery’s control of the $ variables.

We can use it by writing:

jQuery.noConflict();
jQuery( "div p" ).hide();

We call noConflict .

Then we can invoke jQuery with jQuery instead of $ .

jQuery.noop()

The jQuery.noop() method returns an empty function.

jQuery.param()

The jQuery.param() method converts an object’s key-value pairs into a query string.

For example, we can write:

const params = {
  width: 1680,
  height: 1050
};
const str = jQuery.param(params);
console.log(str)

Then str is ‘width=1680&height=1050’ .

jQuery.parseHTML()

The jQuery.parseHTML() method lets us parse a string into an array of DOM nodes.

For example, if we have:

<div id="log">
  <h3>Content:</h3>
</div>

Then we can append the parsed HTML into the div with ID log by writing:

const str = "hello, <b>my name is</b> jQuery.";
const html = $.parseHTML(str);
$("#log").append(html);

We parse the HTML string with $.parseHTML and then call append to append the item.

jQuery.parseJSON()

We can parse JSON string into an object with the jQuery.parseJSON() the method.

For example, if we have:

const obj = jQuery.parseJSON('{ "name": "John" }');
console.log(obj.name === "John");

Then we parse the string into the obj object.

The obj.name value is 'John' .

So the console log logs true .

jQuery.parseXML()

The jQuery.parseXML() method lets us parse XML into an object.

For example, we can use it by writing:

const xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
  xmlDoc = $.parseXML(xml),
  $xml = $(xmlDoc),
  title = $xml.find("title");
console.log(title.text())

We call parseXML with the XML string.

Then we put that into the $ function so that we can call find to find the element we want.

Then we can get the text of the matched element with the text method.

jQuery.post()

The jQuery.post() method lets us make a POST request to a given URL.

For example, we can write:

const jqxhr = $.post("https://jsonplaceholder.typicode.com/posts", {
    title: "title",
    body: "body"
  }, function() {
    console.log("success");
  }, "json")
  .done(function() {
    console.log("second success");
  })
  .fail(function() {
    console.log("error");
  })
  .always(function() {
    console.log("finished");
  });

jqxhr.always(function() {
  console.log("second finished");
});

to make a POST request.

The 2nd argument is the form data keys and values we want to send with the request.

The done callback is called when the request is successful.

The fail callback is called when the request failed.

always callback is called when the request is finished regardless of the result.

jQuery.queue()

The jQuery.queue() method show or manipulate the queue of functions to be run on the matched element.

For example, if we have the following HTML:

<div></div>

and CSS:

div {
  margin: 3px;
  width: 40px;
  height: 40px;
  position: absolute;
  left: 0px;
  top: 30px;
  background: green;
  display: none;
}

div.newcolor {
  background: blue;
}

span {
  color: red;
}

Then we can animate the div by writing:

$("div")
  .show("slow")
  .animate({
    left: "+=200"
  }, 2000)
  .slideToggle(1000)
  .slideToggle("fast")
  .animate({
    left: "-=200"
  }, 1500)
  .hide("slow")
  .show(1200)

console.log(jQuery.queue($("div")[0], "fx"))

We get the div and use that as the argument of jQuery.queue to check the items that are queued.

Conclusion

We can parse JSON and XML and queue actions with jQuery.