Skip to content

Instantly share code, notes, and snippets.

@jhyang12345
Last active July 12, 2017 00:16
Show Gist options
  • Save jhyang12345/8c45565fbef988ce61686f90e9e71ff6 to your computer and use it in GitHub Desktop.
Save jhyang12345/8c45565fbef988ce61686f90e9e71ff6 to your computer and use it in GitHub Desktop.

Using the argument object to fetch variable length parameters

function foo() {
  for (var i = 0; i < arguments.length; i++) {
    alert(arguments[i]);
  }
}

Iterating through Nodelist

using indexing

for (var i = 0; i < myNodeList.length; ++i) {
  var item = myNodeList[i];  // Calling myNodeList.item(i) isn't necessary in JavaScript
}

for of loop

var list = document.querySelectorAll( 'input[type=checkbox]' );
for (var item of list) {
  item.checked = true;
}

forEach loop

var list = document.querySelectorAll( 'input[type=checkbox]' );
Array.prototype.forEach.call(list, function (item) {
  item.checked = true;
});

1번

//using innerHTML to add elements
var ul = document.querySelector("ul");
ul.innerHTML = ul.innerHTML + "<li>watermelon</li>";

2번

//using insertAdjacentHTML to add elements in a particular order
var banana = document.querySelectorAll('li')[2];
banana.insertAdjacentHTML('beforebegin', '<li>dragonfruit</li>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment