JQuery Flashcards
What function can be used to delay execution of a script until the page is fully loaded?
$(document).ready();
When adding a new element to a page dynamically, you find events are not working for these new items. What could the issue be?
If the event was setup using $(‘id’).click, creating a new element doesn’t seem to bind the event. Use the $(‘body’).on(‘click’ ….) function call instead.
Instead of using $.get, what is another method?
$.ajax({ type: 'GET', url: url, data: data, dataType: 'json' });
What field, in the $.ajax call configures the callback?
success
Given an element with a data attribute as such ‘data-set=”4 ,1”’ - how would you select the element if it had the number 4 as one it’s values?
$(“[data-set~=’4’]”)
How would you load content into a div from a separate HTML file?
$(‘#divid’).load(‘content/mycoolcontent.html’);
Do this once the page has loaded, and the html content will be loaded into the page at the location specified by the #id.
What is the issue with using window.outerWidth to determine screen size?
It doesn’t take into account scroll bars and other such intrusions. The actual content width is usually a bit smaller - and the problem is that media queries use the content width, whereas JavaScript gives you the outerwidth.
How might you write a function that detects screen size change?
function checkBrowserSize() { windowWidth = window.outerWidth; var contentWidth = $('body').width(); var sizeDiff = windowWidth - contentWidth; actualSize = windowWidth - sizeDiff;
if (actualSize > 800) { newWindowSize = 'large'; } else if (actualSize 500) { newWindowSize = 'medium'; } else if (actualSize
When using jQuery to animate, you may find that between media queries, the position of elements are not entirely what was expected. Why might this be?
The jQuery animate function adds a ‘style’ attribute to the targeted elements. This could be overriding the css. So you need to remove that style.
$(this).removeAttr(‘style’); (where ‘this’ is targetting the element with the attribute)
window.outerWidth does not work in IE 7 and 8. How would you go about determining the size of the window?
NOTE: isIE is a variable set by checking support.leadingWhitespace.
if (isIE) { windowWidth = $('body').width() + 33; } else { windowWidth = window.outerWidth; }
The windowWidth will now have a value (we are using jQuery) - although the + 33 is an arbitrary value, we don’t actually know if there is a scroll bar or not.
What is the difference between the ready and the onload event?
onload fires after all images and code has loaded (so it’s blocking). The ready event fires once the DOM has loaded - which means it will fire a lot earlier.
What is the difference between “.html” and “.text” methods?
.html inserts html, whereas .text will insert escaped text, meaning you will see any tags etc in the output.
What JQuery method can you use to stop events from listening?
.off
When you get an array back from a JQuery selector, is the array an array of DOM elements?
No - it’s an array of objects that wrap each of the DOM elements.
What is the difference between a selector and a filter?
Selectors find the elements, whereas filters select from a group of elements (chosen by a selector) based on a more granular requirement. For instance.
$(“p”) - selects all of the p elements.
Adding the filter :first
$(“p:first”) - selects only the first p element
How would you select paragraph elements that have an index greater than 1?
$(“p:gt(1)”)
How could you use a selector to select all of the p elements bar the second one?
$(“p:not(p:eq(2))”)
So we are saying here, grab all elements, but not the one equal to 2.
Can you chain attribute selectors with JQuery?
Yes
$(“p[id^=para][lang*=eng-]”)
We are looking for “p” tags that contain an attribute (id) that starts with the word “para” and a lang attribute that contains the word “eng-“.
What does the :contains filter do?
It looks at the actual contents of the selector, to see if the specified content is in the tag.. i.e.
$(“p:contains(‘superman’)”)
Would select any p elements that contain the words “superman”.
What does the :parent filter do?
Selects all P tags that are parents, i.e. they contain some other tag.
What does the :has filter do?
It allows you to search for an element that has another element with some specific condition. i.e.
$(“div:has(p[class=a])”)
Give me all the divs that contain a paragraph element that has a class with a in it
What would the following selector select?
$(“div p:first-child”)
The first p, that is a child, and within a div.
NOTE: It’s not necessarily the first p that is a child of the div, but a p that is a first-child somewhere inside the div.
How would you select the last p in a div, without using last-child?
$(“div p:last-of-type”)
NOTE: if there are other divs/containers within the outer div, then the p:last-of-type will apply to those containers too. It’s last-of-type at that level.
What is unusual about using nth-child filter?
It’s not 0 based, it starts at 1.