query select javascript code example
Example 1: class query css js all
var matches = document.querySelectorAll("div.note, div.alert");
Example 2: queryselector
var el = document.querySelector(".myclass");
Example 3: js queryselector names
// This selects the first element with that name
document.querySelector('[name="your-selector-name-here"]');
Example 4: queryselector function in javascript
// for example
//for class
document.querySelector(".ClassName")
// for id
document.querySelector("#ID")
// etc.
Example 5: js queryselector
const content = document.querySelector('.content');
const towns = ['Helsinki', 'Espoo', 'Vantaa'];
towns.forEach(city => {
content.innerHTML += `<p>${city}</p>`;
});
Example 6: document.queryselector
<div id="foo\bar"></div>
<div id="foo:bar"></div>
<script>
console.log('#foo\bar'); // "#fooar" (\b is the backspace control character)
document.querySelector('#foo\bar'); // Does not match anything
console.log('#foo\\bar'); // "#foo\bar"
console.log('#foo\\\\bar'); // "#foo\\bar"
document.querySelector('#foo\\\\bar'); // Match the first div
document.querySelector('#foo:bar'); // Does not match anything
document.querySelector('#foo\\:bar'); // Match the second div
</script>