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.
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
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

0
maqskywalkerAuthor Commented:
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?
0
zc2Commented:
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

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

maqskywalkerAuthor Commented:
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'
            });
0
zc2Commented:
Sure,
var tes = document.querySelectorAll("input[id^='txtemployee']");
for( var i = 0; i < tes.length; i++ )
  	tes[i].value='value';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maqskywalkerAuthor Commented:
Thanks.
0
maqskywalkerAuthor Commented:
Thanks , works on chrome and ie just fine.
0
Michel PlungjanIT ExpertCommented:
forEach on nodelists is indeed not supported by any IE
https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.