visibility 66 comment 0 access_time 2 months ago language English

codeVanilla JavaScript - Wildcard (*) Selector

In vanilla JavaScript, we can use wildcard (*) to match multiple elements (nodes) in the HTML DOM object. We can also match elements with attribute starting with, ending with or contains certain values.

For example, the following selectors will 

  • [name^="password"] will match all elements with name starting with password.
  • [name$="password"] will match all elements with name ending with password.
  • [name*="password"] will match all elements with name containing password.

Code snippet

<script type="text/javascript">
let selector = '[name*="password"]';
var elements = document.querySelectorAll(selector);
if(elements)
{
   elements.forEach(el=>{console.log(el.name);});
}
</script>
fork_right Fork
copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

comment Comments
No comments yet.