Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the jQuery-like selector in pure JavaScript

What I looking for is:

var arrinput = $('input[name$="letter"]')

How can I change that from jQuery style to a pure javascript style?

So I want the <input> tags which name is ended with "letter".


I changed code a bit... My browser dont support querySelector and FYI I'm using webbrowser component on c# winforms

like image 558
Mr.Rendezvous Avatar asked Sep 14 '11 03:09

Mr.Rendezvous


2 Answers

For modern browsers:

document.querySelector('input[name$=letter]');

will return the first match.

document.querySelectorAll('input[name$=letter]');

will return a list of matches.

I suspect that if you look through the jquery source code, it uses document.querySelector[All] when it's available.

like image 171
Hemlock Avatar answered Sep 25 '22 13:09

Hemlock


Try:

var items = document.getElementsByTagName('input');
for (var i=0; i<items.length; i++) {
    var item = items[i];
    if (/letter$/.test(item.name)) {
        item.value = "A letter";
    }
}
like image 43
BenjaminRH Avatar answered Sep 25 '22 13:09

BenjaminRH