Skip to content Skip to sidebar Skip to footer

Getelementbyid - Javascript

I'm using the Google autocomplete javascript code for address completion. It tracks the imput field getting the Id document.getElementById('addressField'). The problem I have is t

Solution 1:

Regex not needed! You can use querySelector

document.querySelector("[id*='addressField']")

If I remember correctly, the *= operator in a selector looks for elements that contain addressField

If you are trying to get a nodelist of multiple of these elements, simply use querySelectorAll instead. The former just returns the first node of the list, all returns the whole list.

As Lucas said in the comments: $= will match the end of the attribute. Both will work, not 100% sure if one is better than the other however.

Post a Comment for "Getelementbyid - Javascript"