Send function with JavaScript parameters

0

I have the following code line:

<td border="1" onClick="myFunction()">

I am trying to do the following to be able to receive a value in the function myFunction (variable).

<td border="1" onClick="myFunction(variable)"> 

But you send me the following error:

  

is not defined       at HTMLTableCellElement.onclick

    
asked by ARR 12.07.2018 в 16:42
source

2 answers

1

What you want to do goes as follows:

function myFunction(mensaje) {
  console.log(mensaje);
}
table, th, td {
   border: 1px solid black;
}
<table>
  <tr>
    <td onClick="myFunction('Celda A')">Celda A</td>
    <td onClick="myFunction('Celda B')">Celda B</td>
  </tr>
</table>

Note:

  

If you want to send some type of variable and it gives you an error, you will have to put more code in your question.

    
answered by 12.07.2018 / 17:00
source
0

To use variables, you must stop writing embedded code. Besides a bad practice, the js is more powerful in separate scripts.

Where do you get 'variable' in the html? You must explain more the problem.

    
answered by 12.07.2018 в 19:00