jQuery Alternatives Flashcards

1
Q

hide/show an element

A

el.style.display = ‘none’ or ‘’ or ‘block’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

$el.addClass( ‘someclass’ )

A

if (el.classList)
el.classList.add(className);
else
el.className += ‘ ‘ + className;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

$(el).after(htmlString);

A

el.insertAdjacentHTML(‘afterend’, htmlString);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

$(parent).append(el);

A

someParent.appendChild(el);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

$(el).children();

A

el.children;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

$(selector).each(function(i, el){ …

A
var els = document.querySelectorAll( selector );
Array.prototype.forEach.call( els, function( el, i ) {...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

$(el).html();

A

el.innerHTML or el.innerHTML = ‘some content’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

$(el).text();

A

el.textContent or el.textContent = ‘something’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

$(el).next();

A

el.nextElementSibling;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

$(el).prev();

A

el.previousElementSibling;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

$(el).parent();

A

el.parentNode;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

$(parent).prepend(el);

A

parent.insertBefore(el, parent.firstChild);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

$(el).toggleClass(className);

A

if (el.classList) {
el.classList.toggle(className);
} else { // use className

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

$(el).on(eventName, eventHandler);

A

el.addEventListener(eventName, eventHandler);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

$(document).append(“<div class="hello">Hi There!</div>

A
var d = document.createElement("div");
d.classList.add("one");
var t = document.createTextNode("Hi There!");
d.appendChild(t);
document.body.appendChild(d);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How could you create and insert some new text saying “Hi World” into a new div tag as the first element in the body, before all other elements?

A
var text = document.createTextNode("Hi World");
var el = document.createElement("div");
el.appendChild(text);
document.body.insertBefore(el, body.firstChild);
// insertBefore takes the element to be inserted and the location.
17
Q

How can you get a copy of a node called myNode? What if you want all of that nodes children as well?

A
var clone = myNode.cloneNode(false); // just the top node
var cloneDeep = myNode.cloneNode(true); // children as well.
18
Q
var main = document.getElementById("main");
How could you access main's first child?
A

main.firstChild or main.childNodes[0];

19
Q

Alternative for $(document).ready()

A
function ready(fn) {
      if (document.readyState != 'loading'){
        fn();
      } else {
        document.addEventListener('DOMContentLoaded', fn);
      }
    }