-
Notifications
You must be signed in to change notification settings - Fork 340
jQuery
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
For some reason FCC likes to introduce jQuery
before JavaScript itself.
To add JS to your HTML, you need a script
tag. Your browser will run any JavaScript inside a script element, including jQuery.
Inside your script element, add this code: $(document).ready(function() {
to your script. Then close it on the following line (still inside your script element) with: });
<script> $(document).ready(function(){
});</script>
After we have created our document ready function
we can now have code that will run after the page loads. This will assure that your code does not run before the HTML is rendered to avoid bugs.
jQuery often selects an HTML element with a selector, then does something to that element.
The following code will make the buttons have a bouncy animation on page load.
<script>
$(document).ready(function() {
$("button").addClass("animated bounce");
});
</script>
Just as we did before, we can also target elements by classes.
$(".well").addClass("animated shake");
You can also target elements by their id attributes. Note that, just like with CSS declarations, you type a # before the id's name.
$("#target3").addClass("animated fadeOut");
To delete the functions it is just as any other piece of code that you want to remove, select it and delete with the keyboard.
The multiple jQuery selectors are:
- By type:
$("button")
- By class:
$(".btn")
- By id:
$("#target1")
<script>
$(document).ready(function() {
$("button").addClass("animated");
$(".btn").addClass("shake");
$("#target1").addClass("animated shake btn-primary");
});
</script>
The same way we can add classes using jQuery, we an also remove them with removeClass()
.
$("button").removeClass("btn-default");
We can also change the CSS of an HTML element directly with jQuery.
Query has a function called .css()
that allows you to change the CSS of an element.
<script>
$(document).ready(function() {
$("#target1").css("color", "red");
});
</script>
You can also change the non-CSS properties of HTML elements with jQuery. For example, you can disable buttons.
When you disable a button, it will become grayed-out and can no longer be clicked.
jQuery has a function called .prop()
that allows you to adjust the properties of elements.
Here's how you would disable all buttons: $('#button').prop('disabled', true);
jQuery has a function called .remove()
that will remove an HTML element entirely. $("#target4").remove();
jQuery has a function called appendTo()
that allows you to select HTML elements and append them to another element.
For example, if we wanted to move target4 from our right well to our left well, we would use $("#target4").appendTo("#left-well");
jQuery has a function calledclone()
that makes a copy of an element.
For example, if we wanted to copy target2 from our left-well to our right-well, we would use $("#target2").clone().appendTo("#right-well");
Every HTML elements has a parent element from which it inherits properties.
For example, your jQuery Playground h3
element has the parent element of<div class="container-fluid">
, which itself has the parent body.
jQuery has a function called parent()
that allows you to access the parent of whichever element you've selected.
Here's an example of how you would use the parent()
function: $("#left-well").parent().css("background-color", "blue")
Many HTML elements have children elements from which they inherit properties.
jQuery has a function called children()
that allows you to access the children of whichever element you've selected.
Here's an example of how you would use the children()
function: $("#left-well").children().css("color", "blue");
jQuery uses CSS Selectors to target elements. target:nth-child(n)
css selector allows you to select all the nth element with the target class or element type.
Here's how you would give the third element in each well bounce: $(".target:nth-child(3)").addClass("animated bounce");
$(".btn:nth-child(2)").addClass("animated bounce");
You can also target all the even-numbered elements.
Here's how you would target all the odd-numbered
elements with class target and give them classes: $('.target:odd').addClass('animated shake');
This will shake all the even ones: $('.target:even').addClass("shake");
jQuery can target the body element as well.
Here's how we would make the entire body fade out: $('body').addClass('animated fadeOut');
<script>
$(document).ready(function() {
$("#target1").css("color", "red");
$("#target1").prop("disabled", true);
$("#target4").remove();
$("#target2").appendTo("#right-well");
$("#target5").clone().appendTo("#left-well");
$("#target1").parent().css("background-color", "red");
$("#right-well").children().css("color", "green");
$("#left-well").children().css("color", "green");
$(".target:nth-child(2)").addClass("animated bounce");
$(".target:even").addClass("animated shake");
$("body").addClass("animated hinge");
});
</script>
<!-- You shouldn't need to modify code below this line -->
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<h4>#left-well</h4>
<div class="well" id="left-well">
<button class="btn btn-default target" id="target1">#target1</button>
<button class="btn btn-default target" id="target2">#target2</button>
<button class="btn btn-default target" id="target3">#target3</button>
</div>
</div>
<div class="col-xs-6">
<h4>#right-well</h4>
<div class="well" id="right-well">
<button class="btn btn-default target" id="target4">#target4</button>
<button class="btn btn-default target" id="target5">#target5</button>
<button class="btn btn-default target" id="target6">#target6</button>
</div>
</div>
</div>
</div>
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3