Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

JavaScript selector

On button click event how do I assign a variable value to textbox whose Id starts with txtemployee
If

I want to do it with JavaScript, not jQuery.

See I have 3 texboxes.

They are called:

txtemployee1
txtemployee2
txtemployee3

Normally I do this with jquery selector. But I'm trying to learn to do the same only using JavaScript.
Avatar of zc2
zc2
Flag of United States of America image

Try this:
https://jsfiddle.net/23wx9nar/7/

	var tes = document.querySelectorAll("input[id^='txtemployee']");
  tes.forEach(function(te) {
  	te.value='value';
  });

Open in new window

Avatar of maqskywalker
maqskywalker

ASKER

I tried it with chrome and it works, but I tried it with Internet Explorer 11 and it's not working.
Do you know a better way so it works on both Chrome and Internet Explorer?
probably the selector by attribute value's substring  is not  supported. Can you assign an unique class to all those input boxes (say "txtemployee_class") and select them with
var tes = document.querySelectorAll("input.txtemployee_class");

Open in new window

I don't think the issue is by id or by class. I tried by class and still get the same error in internet explorer.

The error message i get in internet explorer is :  Object doesn't support property or method 'forEach'

I think IE doesn't support foreach.
https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/


Is there a way to re-write this part without using forEach?

            tes.forEach(function (te) {
                te.value = 'value'
            });
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.
Thanks , works on chrome and ie just fine.
forEach on nodelists is indeed not supported by any IE
https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach