Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

apply javascript eventlistener on multiple textboxes whose id starts with

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?
ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco 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
Avatar of maqskywalker
maqskywalker

ASKER

Nice solution!
Glad I could help.