We help IT Professionals succeed at work.

apply javascript eventlistener on multiple textboxes whose id starts with

maqskywalker
maqskywalker asked
on
163 Views
Last Modified: 2018-12-18
I'm working with JavaScript eventlistener

Right now I have this:

            // ----- Event Listener - TextBox Click -----
            document.getElementById('DataGridA_txtBox1').addEventListener('click', function () {

                // get value of textbox and save in variable
                var x = document.getElementById("DataGridA_txtBox1").value;

                // after clicking in textbox save existing textbox value in hiddenfield1
                document.getElementById('HiddenField1').value = x;

            })
            // ----- Event Listener - TextBox Click -----


The above event listener works fine.
When I click in a Textbox called DataGridA_txtBox1, then the value of the textbox is stored in HiddenField1


What if I have 3 textbox on my page.

The texboxes on my page would be called this:

DataGridA_txtBox1
DataGridA_txtBox2
DataGridA_txtBox3

I only want to have one event listener.

Is there a way so revise my click event so that the click event applies to any textbox whose id starts with DataGridA_txtBox?
Comment
Watch Question

Analyst Developer
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Nice solution!
Zakaria AcharkiAnalyst Developer
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Glad I could help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.