a Insert onclick in tag

Asked 2 years ago, Updated 2 years ago, 32 views

<a href="example.com" class="a">

I would like to add an onclick event using regular expressions to a tag like the one above.
How will it work?
Please let me know if you know more.Thank you for your cooperation.

javascript php html

2022-09-29 21:55

1 Answers

It's not a regular expression, but if you use the querySelectorAll function, it looks like this.

function someFunction(event){
    console.log(event);
}

varaList= document.querySelectorAll('a.a');

for (vari=0;i<aList.length;i++) {
  aList[i].onclick=someFunction;
}
<a href="example.com" class="a">example.com</a>
<a href="sub.example.com" class="a">sub.example.com</a> 


2022-09-29 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.