0

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.

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Nice, was hoping for something like that.
So, did I get This right, It takes the entire element, and how would I get It's ID?
Yes, this gives you the entire object. Use the id property of the object to get the value you want. See my updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.