I use this code to get value from an input box:
var suggest_type = document.getElementById('ac-type').value;
Now I need to apply my js code on several other pages. I heard that it’s not nice to repeat an ID on one website. So, I’m thinking to change to use class like this:
var suggest_type = document.getElementByClass('ac-type').value;
This doesn’t get the value. How can I use class to get value?
You should stick with using ID’s – they only have to be unique within a page.
If you must use classes, you need to use
getElementsByClassName():However this function is not well supported on older browsers, which is another good reason to stick with IDs.