Categories
jQuery

jQuery — Images and Inserting Elements

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.

:image Selector

We can select any element with type image with the :image selector.

For example, if we have:

<form>  
  <input type="button" value="Input Button">  
  <input type="checkbox">  
  <input type="file">  
  <input type="hidden">  
  <input type="image">  
</form>

Then we can write:

$("input:image").css({  
  background: "yellow",  
  border: "3px red solid"  
});

to add a yellow background and a red border to the image input.

.index()

The index method lets us get the index of an element.

For example, if we have:

<ul>  
  <li id="foo">foo</li>  
  <li id="bar">bar</li>  
  <li id="baz">baz</li>  
</ul>

Then we can use it by writing:

const listItems = $("li").slice(1);  
console.log($("li").index(listItems));

Then the console log should log 1 since we got the first item on the list.

.innerHeight()

The innerHeight method gets the current computed inner height for the first element in the set of matched elements.

It can also set the inner height of every matched element.

For example, if we have:

<p>Hello</p>

Then we can get the inner height of the p element by writing:

console.log($("p").first().innerHeight());

If we have:

<div>d</div>  
<div>d</div>  
<div>d</div>  
<div>d</div>  
<div>d</div>

Then we can set the inner height by writing:

const modHeight = 70;  
$("div").one("click", function() {  
  $(this).innerHeight(modHeight).addClass("mod");  
});

.innerWidth()

The innerWidth method lets us set the inner width of an element for the first element in a set of matched elements.

It can also be used to set the inner width of every matched element.

For example, if we have:

<p>Hello</p>

Then we can get the inner width of the p element by writing:

const p = $("p").first();  
console.log(p.innerWidth())

If we have:

<div>d</div>  
<div>d</div>  
<div>d</div>  
<div>d</div>  
<div>d</div>

And the following CSS:

.mod {  
  background: blue;  
  cursor: default;  
}

Then we can write:

const modWidth = 70;  
$("div").one("click", function() {  
  $(this).innerHeight(modWidth).addClass("mod");  
});

to set the inner width of the div when we click it.

:input Selector

The :input selector lets us select all input, textarea, select, and button elements.

For example, if we have:

<form>  
  <input type="button" value="Input Button">  
  <input type="checkbox">  
  <input type="file">  
  <input type="hidden">  
  <input type="image">  
  <input type="password">  
  <input type="radio">  
  <input type="reset">  
  <input type="submit">  
  <input type="text">  
  <select>  
    <option>Option</option>  
  </select>  
  <textarea></textarea>  
  <button>Button</button>  
</form>

Then we can write:

const allInputs = $(":input");  
console.log(allInputs)

to get all the elements in the form.

.insertAfter()

The .insertAfter() method inserts every element in the set of the matched elements after the target.

For example, if we have:

<div class="container">  
  <h2>Greetings</h2>  
  <div class="inner">Hello</div>  
  <div class="inner">Goodbye</div>  
</div>

Then we can add an element after each div with the class inner by writing:

$("<p>Test</p>").insertAfter(".inner");

.insertBefore()

We can call the insertBefore method to insert every element in the set of matched elements before the target.

For example, if we have:

<div class="container">  
  <h2>Greetings</h2>  
  <div class="inner">Hello</div>  
  <div class="inner">Goodbye</div>  
</div>

We can insert an element before each div with the class inner by writing:

$("<p>Test</p>").insertBefore(".inner");

Conclusion

We can insert elements and get images elements with jQuery.

Categories
jQuery

jQuery — Height, Hide, ID, and More

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.

.height()

The .height() method gets the current computed height for the first element in the set of matched elements.

It can also ser the height of every matched element.

For example, we can get the height of window by writing:

console.log($(window).height())

And if we have a div:

<div></div>

with the following CSS:

div {
  width: 50px;
  height: 70px;
  float: left;
  margin: 5px;
  background: red;
  cursor: pointer;
}

We can set its height when we click it by writing:

$("div").one("click", function() {
  $(this).height(30).css({
    cursor: "auto",
    backgroundColor: "green"
  });
});

The height is in pixels.

:hidden Selector

We can use the :hidden select to select all elements that are hidden.

For example, if we have:

<div></div>
<div style="display:none;">Hidden</div>
<div></div>

<div class="starthidden">Hidden</div>
<div></div>

<form>
  <input type="hidden">
  <input type="hidden">
  <input type="hidden">
</form>

and the following CSS:

.starthidden {
  display: none;
}

Then we can get all the hidden elements that aren’t script elements buy writing:

const hiddenElements = $("body").find(":hidden").not("script");
console.log(hiddenElements)

We can also show the hidden divs by writing:

$("div:hidden").show(3000);

.hide()

The .hide() method hides the matched elements.

For example, if we have:

<div id="clickme">
  Click here
</div>
<img id="book" src="https://i.picsum.photos/id/23/200/200.jpg?hmac=IMR2f77CBqpauCb5W6kGzhwbKatX_r9IvgWj6n7FQ7c" alt="">

Then we can hide the image when we click Click here by writing:

$("#clickme").click(function() {
  $("#book").hide("slow", function() {
    alert("Animation complete.");
  });
});

The first argument of hide is the animation tup;e.

And the 2nd argument is the callback that’s called when the animation is done.

.hover()

The hover method lets us bind event handlers for hovering over and hover out of the element.

For example, if we have:

<ul>
  <li>Milk</li>
  <li>Bread</li>
  <li class="fade">Chips</li>
  <li class="fade">Socks</li>
</ul>

Then we can add *** to the li text when we hover over it by writing:

$("li").hover(
  function() {
    $(this).append($("<span> ***</span>"));
  },
  function() {
    $(this).find("span").last().remove();
  }
);

$("li.fade").hover(function() {
  $(this).fadeOut(100);
  $(this).fadeIn(500);
});

The *** will disappear when we move our mouse outside.

.html()

The .html() method gets the HTML content of the first element in the set of matched elements.

It can also be used to set the HTML of every matched element.

For example, if we have:

<p>
  <b>Click</b> to change the <span id="tag">html</span>
</p>

Then we can click on the p element to change the text to its raw HTML form by writing:

$("p").click(function() {
  const htmlString = $(this).html();
  $(this).text(htmlString);
});

We call html in the first line to get the HTML code, then we set that as the text value.

It’ll be escaped when we set it as the text content so that we’ll see the raw code displayed.

ID Selector (“#id”)

We can select a single element with the ID selector.

For example, if we have:

<div id="myDiv">id="myDiv"</div>

Then we can add a red border to the div by writing:

$("#myDiv").css("border", "3px solid red");

Conclusion

We can set the height, hide elements, get its HTML code, and more with jQuery.

Categories
jQuery

jQuery — Focus and Has Selectors

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.

.focusin()

The .focusin() method binds an event handler to the focusin event.

For example, if we have:

<p><input type="text"> <span>focusin fire</span></p>
<p><input type="text"> <span>focusin fire</span></p>

Then we can write:

$("p").focusin(function() {
  $(this).find("span").css("display", "inline").fadeOut(1000);
});

We get the span and fade it out when we focus on the input inside the p element.

.focusout()

The focusout method lets us bind an event handler to the focusout JavaScript event.

For example, if we have:

<p><input type="text"> <span>focusout fire</span></p>
<p><input type="text"> <span>focusin fire</span></p>

We can write:

let focus = 0;
$("p")
  .focusout(function() {
    focus++;
    console.log(focus);
  })

to count the number of times the focusout event is triggered.

It’ll be triggered when we move focus on out of the p element.

.get()

The get method retrieves the DOM element matched by the jQuery object.

For example, if we have:

<ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
</ul>

We get the first li element by writing:

console.log($("li").get(0));

.has()

The has method lets us reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.

For example, if we have:

<ul>
  <li>list item 1</li>
  <li>list item 2
    <ul>
      <li>list item 2-a</li>
      <li>list item 2-b</li>
    </ul>
  </li>
  <li>list item 3</li>
  <li>list item 4</li>
</ul>

We can write:

$("li").has("ul").css("background-color", "red");

to get all the li ‘s that have ul ‘s inside and add a red background to it.

Has Attribute Selector [name]

We can use the has attribute selector to get all the elements with the given attribute set.

For example, if we have:

<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
<div>nope</div>

Then we can get all the elements with the id attribute set by writing:

$("div[id]").one("click", function() {
  const idString = $(this).attr("id");
  $(this).text(idString);
});

In the callback, we set the text of the currently clicked on element’s text content to the value of the id attribute.

:has() Selector

The :has selector lets us get all the elements that has at least one element that matches the specified selector.

For example, if we have the following HTML:

<div>
  <p>paragraph</p>
</div>
<div>Hello again! (with no paragraph)</div>

and the following CSS:

.test {
  border: 3px inset red;
}

Then we can add a border to the div with a p element in it by writing:

$("div:has(p)").addClass("test");

.hasClass()

The .hasClass() method determines whether any matched elements are assigned the given class.

For example, if we have:

<div id="mydiv" class="foo bar"></div>

Then we can check if the div with ID mydiv has the foo class added to it by writing:

$("#mydiv").hasClass("foo")

:header Selector

The :header selector selects all elements that are headers (e.g. h1, h2, etc.)

For example, if we have:

<h1>Header 1</h1>
<p>Contents 1</p>
<h2>Header 2</h2>
<p>Contents 2</p>

Then we can write:

$(":header").css({
  background: "gray",
  color: "blue"
});

to add a gray background and make the text blue to the h1 and h2 elements.

Conclusion

We can select all header elements and get elements with various selectors with jQuery.

Categories
jQuery

jQuery — First Elements and Focus

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.

:first-child Selector

We can use the :first-child selector to get the first child element.

For example, if we have the following HTML:

<div>
  <span>Glen,</span>
  <span>Jane,</span>
  <span>Ralph</span>
</div>

And the following CSS:

.green {
  color: green
}

Then we can write:

$("div span:first-child")
  .css("text-decoration", "underline")
  .hover(function() {
    $(this).addClass("green");
  }, function() {
    $(this).removeClass("green");
  });

to get the first span in the div with the :first-child selector.

Then we called css to add the underline to the first span.

And finally we add hover to it with hover .

The first callback is called when we hover over the element, and the second one is called when we hover away from it.

:first-of-type Selector

We can use the :first-of-type selector to select all elements that are the first among the siblings with the same element name.

For example, if we have the following HTML:

<div>
  <span>Glen,</span>
  <span>Tane,</span>
  <span>Ralph</span>
</div>
<div>
  <b>Nobody,</b>
  <span>John,</span>
  <span>Scott,</span>
  <span>Tim</span>
</div>

and the following CSS:

.green {
  color: green
}

Then we can add the green class to the first span in each div by writing:

$("span:first-of-type").addClass("green");

:first Selector

The :first select lets us select the first matched DOM element.

For example, if we have:

<table>
  <tr>
    <td>Row 1</td>
  </tr>
  <tr>
    <td>Row 2</td>
  </tr>
  <tr>
    <td>Row 3</td>
  </tr>
</table>

Then we can make the first tr italic by writing:

$("tr:first").css("font-style", "italic");

Now everything in the tr is italic.

.focus()

The focus method lets us add an event handler for the focus event or trigger the event on an element.

For example, if we have:

<form>
  <input id="target" type="text" value="Field 1">
  <input type="text" value="Field 2">
</form>
<div id="other">
  Trigger the handler
</div>

Then we can add a callback that’s called when the focus event is triggered on the input with ID target by writing:

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

:focus Selector

The :focus selector selects an element if it’s currently focused.

For example, if we have the following HTML:

<div id="content">
  <input tabIndex="1">
  <input tabIndex="2">
  <select tabIndex="3">
    <option>select menu</option>
  </select>
  <div tabIndex="4">
    a div
  </div>
</div>

And the following CSS:

.focused {
  background: green;
}

Then we can add the green class to element that’s focused by writing:

$("#content").delegate("*", "focus blur", function() {
  const elem = $(this);
  setTimeout(function() {
    elem.toggleClass("focused", elem.is(":focus"));
  }, 0);
});

The delegate method’s callback has the child elements of the div with ID content as the value of this .

And the callback is called when focus or blur event is triggered on any child element in it.

So we can use toggleClass to toggle the green class on when the element is focused.

The first argument is the class name and the second argument is the condition that toggles on the class in the first argument.

Conclusion

We can select elements with various selectors and focus elements with jQuery.

Categories
jQuery

jQuery - Find Elements and Finish Animations

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.

:file Selector

We can get the input with type file with the :file selector.

For example, if we have:

<form>  
  <input type="button" value="Input Button">  
  <input type="checkbox">  
  <input type="file">  
  <input type="hidden">  
  <input type="password">  
  <input type="radio">  
  <input type="reset">  
  <input type="submit">  
  <input type="text">  
  <select>  
    <option>Option</option>  
  </select>  
  <textarea></textarea>  
  <button>Button</button>  
</form>

We can write:

$("input:file").css({  
  background: "yellow",  
  border: "3px red solid"  
});

.filter()

The filter method lets us get the elements that match the select or pass a function test from a set of matched elements.

For example, if we have:

<ul>  
  <li>list item 1</li>  
  <li>list item 2</li>  
  <li>list item 3</li>  
  <li>list item 4</li>  
  <li>list item 5</li>  
  <li>list item 6</li>  
</ul>

We can get the li s that are in an even number position and add a red background to them by writing:

$("li").filter(":nth-child(2n)").css("background-color", "red");

So the 2nd, 4th, and 6th li elements have a red background.

.find()

The .find() method gets the descendants of each element in the current set of matched elements filtered by a selector, jQuery object, or element.

For example, if we have:

<ul class="level-1">  
  <li class="item-i">I</li>  
  <li class="item-ii">II  
    <ul class="level-2">  
      <li class="item-a">A</li>  
      <li class="item-b">B  
        <ul class="level-3">  
          <li class="item-1">1</li>  
          <li class="item-2">2</li>  
          <li class="item-3">3</li>  
        </ul>  
      </li>  
      <li class="item-c">C</li>  
    </ul>  
  </li>  
  <li class="item-iii">III</li>  
</ul>

Then we can get the li ‘s from the li with the class item-ii and add a red background to them by writing:

$("li.item-ii").find("li").css("background-color", "red");

.finish()

The .finish() method stops the currently-running animation, remove all queued animations, and complete all animations for the matched elements.

For example, if we have the following HTML:

<div class="box"></div>  
<div id="path">  
  <button id="go">Go</button>  
  <br>  
  <button id="bf" class="b" onclick="finish()">finish</button>  
</div>

And CSS:

.box {  
  position: absolute;  
  top: 10px;  
  left: 10px;  
  width: 15px;  
  height: 15px;  
  background: black;  
}

Then we can create an animation for the div withn class box when we click on the Go button by writing:

const horiz = $("#path").width() - 20,  
  vert = $("#path").height() - 20;  
$("#go").on("click", function() {  
  $(".box")  
    .clearQueue()  
    .stop()  
    .css({  
      left: 10,  
      top: 10  
    })  
    .animate({  
      top: vert  
    }, 3000)  
    .animate({  
      left: horiz  
    }, 3000)  
    .animate({  
      top: 10  
    }, 3000);  
});

const finish = () => {  
  $("div.box").finish();  
}

The finish function is called when we click on the finish button to skip the animation to the end.

.first()

The .first() method gets the first element from a set of matched elements.

For example, if we have:

<ul>  
  <li>list item 1</li>  
  <li>list item 2</li>  
  <li>list item 3</li>  
  <li>list item 4</li>  
  <li>list item 5</li>  
</ul>

Then we get the first li and add a red background to it by wiring:

$("li").first().css("background-color", "red");

Conclusion

We can get various elements and finish animation with jQuery.