Display name of textbox | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Display name of textbox

I have multiple text boxes on a form and I want to display the name of the textbox when I click on it in a specific textbox. As I have 300 textboxes I dont want to create 300 click Events. Do I need some kind of loop

8th Aug 2019, 7:47 AM
James Marianne
James Marianne - avatar
1 Answer
+ 1
You could give each text box its own id if you haven't already, give the display box an id too, then write a function that sets the text of the display box to something like this.idOfTextBoxYouClicked. All you have to do is set the id of the text box you're focused on as the text of the display box. Or, you could even put them in a div together, and apply the onclick to the div (but I think you might have to target the id differently by traversing the DOM - maybe target child elements of the div?)
8th Aug 2019, 8:07 AM
Rincewind
Rincewind - avatar