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.
JavaScript

Avatar of undefined
Last Comment
Michel Plungjan
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?
Avatar of zc2
zc2
Flag of United States of America image

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

Avatar of maqskywalker
maqskywalker

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of maqskywalker
maqskywalker

ASKER

Thanks.
Avatar of maqskywalker
maqskywalker

ASKER

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
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo