user6234002
user6234002

Reputation: 2595

Pass variable with set value to a function in JavaScript

I have a little span element which I CSSed the f*** out of so that it appears as a switch. It kind of like this:

<Span class="switchbody" value="on"><span class="switchcover">on</span></span>

I wanted to know, if I added an onclick="editswitch()" to the first span element, would it be possible if I passed a Variable containing a unique value so that the JavaScript function knows which switch I am talking about without me having to write a million functions doing exactly the same thing to different elements?

If it is possible, how would I do that

PS I have tried

<span onclick="getId = "switch one";">

If it is possible, no jquery or php, etc. Just JavaScript.

Upvotes: 0

Views: 570

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25351

Use this to refer to the object that fired the event. For example:

function editswitch(me) {
  alert(me.id);
}
<span id="span1" onclick="editswitch(this)">I'm the first span</span>
<br/>
<span id="span2" onclick="editswitch(this)">I'm the second span</span>

Upvotes: 1

Related Questions