How to reuse a JS function on different HTML elements

Samuel Fyckes

I have 2 squares in a <table> and I want that when I click one, it become black and the other don't.

My current code is working but I gave them the same function and id, so when I click the first, it work well, but when I click the second, it's the first one that changes.
I know whats wrong, but I don't know how to correct it without having to create a function for each id.

Let's say I want 100 squares, I won't write one function to each one, so what can I do?

function myFunc() {
  var element = document.getElementById("cc");
  element.classList.toggle("th");
}
table,
th,
td {
  border: solid black 1px;
  width: 100px;
  height: 100px;
}

.th {
  background-color: black;
}
<table>
  <tr>
    <th onclick="myFunc(this)" id="cc"></th>
    <th onclick="myFunc(this)" id="cc"></th>
  </tr>
</table>

Calvin Nunes

First of all, and more important: id must be unique, thats why it is called id (identifier).

Said that, I will show you two options to solve your question:

1. The better option:
Don't add any inline (direct in HTML) onclick listener. Add a common class to all <th>, then in Javascript add a single listener to each element that has the class, and use this inside the function, since this scope will be the clicked element.

Example:

let allTh = document.querySelectorAll(".clickableTh").forEach(x => x.onclick = myFunc)


function myFunc() {
   this.classList.toggle("th");
}
table, th, td {
   border:solid black 1px;     
   width:100px;
   height:100px;
}
.th {
  background-color:black;
}
<table>
  <tr>
    <th class="clickableTh"></th>
    <th class="clickableTh"></th>
    <th class="clickableTh"></th>
    <th class="clickableTh"></th>
    <th class="clickableTh"></th>
    <th id="cc" class="notClickableTh"></th>
  </tr>
</table>

2. Keeping your current structure:
As said in the comments, you pass this as parameter (onclick="myFunc(this)"), then inside myFunc you don't need to find the element, you'll already have it in as parameter.

Click below to see the snippet code of example

function myFunc(elem) {
   elem.classList.toggle("th");
}
table, th, td {
   border:solid black 1px;     
   width:100px;
   height:100px;
}
.th {
  background-color:black;
}
<table>
  <tr>
    <th onclick="myFunc(this)"></th>
    <th onclick="myFunc(this)"></th>
    <th onclick="myFunc(this)"></th>
    <th onclick="myFunc(this)"></th>
    <th onclick="myFunc(this)"></th>
    <th id="cc"></th>
  </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to reuse function in different files?

reuse function with different variables

How to reuse any class or function in react js

How to reuse HTML snippets but change IDs of elements within them

Jmeter: How to reuse custom java function between different JMX file

How can I reuse a function for many different ID values?

How to reuse same HTML page between components with different routing?

How to reuse same button with same value but different ID HTML, ReactJS

Reuse HTML but bind different model

How to reuse an output of a function?

Reuse SVG elements with different path values

How to apply a JavaScript function to different elements by assigning the function to different buttons

How to use same function for different elements in React?

how to spesify certain modals for different elements in html?

How to add js code to different appended elements?

Best way to reuse JavaScript for common html elements

jQuery: How to trigger different actions with the same click function (with different elements)

Is there are way to reuse a function with different values in a code?

Reuse function in two different controllers in angularJS

How can I make all relative URLs on a HTML page be relative to a different domain then it's on (in HTML elements as well as JS HTTP Requests)?

Use link function in directives to append different HTML elements

How to reuse argument from a function?

How to reuse code in an R function?

how to reuse a function in multiple controllers

How to name and reuse a javascript function

How to make a loop to reuse a function

How to reuse the call function in r?

how to call css class as function in html elements?

how can I reuse the function to update different parts of the state of a React component?