JavaScript ID starts with selector with TextBox

Hi experts,

I'm working with JavaScript.

Example 1

I have this fidddle.
http://jsfiddle.net/j4zbdmfw/15/

It works fine.
When you type a comma in the textbox. It replaces it with comma followed by a space.
In the above example applying the replace to a TextBox with ID of TextBox1

Example 2

Now here is my other example.
http://jsfiddle.net/wrqsn45y/

I have 4 text boxes.

How do I apply the replace on every TextBox whose id starts with TextBox1 ?

So the replace should only get applied to the first 3 textboxes.

In jQuery I can do this with a selector. But how do i do this with 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.

MishaProgrammerCommented:
Try something like this code:
document.querySelectorAll('input').forEach(function(elem) {
    elem.addEventListener("keyup", function() {
      	this.value = this.value.replace(/,([^\= ])/g, ', $1');
    });
});

Open in new window

In this code I try to add EventListener to each elements from querySelectorAll. In your case it is all textboxes.

If you plan to use other textBoxes, you can use selectors with other conditions:
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
0
maqskywalkerAuthor Commented:
Misha,

Can't use forEach because forEach doesn't work with Internet Explorer.

Is there a way to write this using a regular for loop?

document.querySelectorAll('input').forEach(function(elem) {
    elem.addEventListener("keyup", function() {
            this.value = this.value.replace(/,([^\= ])/g, ', $1');
    });
});

I tried your code. It works fine with chrome but not with Internet Explorer because IE doesn't support forEach.
0
MishaProgrammerCommented:
Try this code:

var divs = document.querySelectorAll('input'), i;
for (i = 0; i < divs.length; ++i) {
divs[i].addEventListener("keyup", function() {
            this.value = this.value.replace(/,([^\= ])/g, ', $1');
    });
}

Open in new window


Or this code:

[].forEach.call(
  document.querySelectorAll('input'), 
  function(elem){
     elem.addEventListener("keyup", function() {
            this.value = this.value.replace(/,([^\= ])/g, ', $1');
    });
  }
);

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. The first snippet did the trick. Nice!
0
MishaProgrammerCommented:
Glad to help you !
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.