I have an HTML page. I would like to extract all elements whose name starts with “q1_”.
How can I achieve this in JavaScript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A quick and easy way is to use jQuery and do this:
That will grab all elements whose name attribute starts with ‘q1_’. To convert the resulting collection of jQuery objects to a DOM collection, do this:
see http://api.jquery.com/attribute-starts-with-selector/
In pure DOM, you could use
getElementsByTagNameto grab all input elements, and loop through the resulting array. Elements withnamestarting with ‘q1_’ get pushed to another array: