How can I get an input element whose name attribute ends with a particular value using just javascript?
I found this $('input[name$="value"]')
method available in jQuery.
Is there a function like that available in javascript? I cannot use jQuery for some reason.
In JavaScript, we can get user input like this: var name = window. prompt("Enter your name: "); alert("Your name is " + name); The code above simply prompts the user for information, and the prints out what they entered in.
In jQuery, you can listen to events for dynamically added elements using the on() function. The equivalent in JavaScript is addEventListener() function.
Answer: Use the Attribute Selector You can use the CSS attribute selectors to select an HTML element by name using jQuery. The attribute selectors provide a very flexible and powerful mechanism for selecting elements.
I'm surprised that nobody has mentioned this yet. Have you tried:
var elements = document.querySelectorAll('input[name$="value"]');
This will only work if the browser supports the querySelectorAll
method but it is what jQuery uses underneath if support is found.
Here's the table that lists browser support for the method:
When can I use querySelector/querySelectorAll?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With