3

In function clickCircle, i want to use the li which is being clicked. So i want to receive it as a parameter.

<li class="circle" onClick='clickCircle(this)'></li>

But what should i send as an actual paramenter? ie in place of 'this'.

5
  • function clickCircle(chooseSomeNameThatMakesYouHappy) {...} Commented Jul 19, 2013 at 19:10
  • your code should work as you requested, what is not working exactly? Commented Jul 19, 2013 at 19:10
  • 2
    this is what you want to send. Commented Jul 19, 2013 at 19:11
  • Since you can use jquery, you should bind the click handler in js so it is unobtrusive. The clicked element in jquery would be $(this) Commented Jul 19, 2013 at 19:12
  • I downvoted because this is the most basic of client-side language tutorials. Chapter 1, lesson 1! Commented Jul 19, 2013 at 19:14

4 Answers 4

9

You can use this:

function clickCircle(obj) // the li element clicked in the current scope
{
    var element = obj;  // This is the DOM object being clicked
    var $this = $(obj); // This is the jQuery object being clicked

    // Use DOM object like
    element.style.color="#ff0000";

    // Use jQuery object like 
    $this.css('color', 'red');
}
Sign up to request clarification or add additional context in comments.

2 Comments

I had to Google this because I made the mistake of using "this" in the function declaration, so that errored out but that was just a brain fart for me...
You are awesome :)
0

Try this instead

<li class="circle"></li>

$('.circle').on('click', function(event) {
  event.target // your element
});

12 Comments

The question isn't about jQuery
You could've submitted another answer to don't carry over the downvotes.. And as you edited it while it was deleted, I'm not able to remove my downvote as it counts as not edited (seems like a bug). Or maybe it was because you edited it inside the 5 mins grace period.
@edi9999 it's labeled jQuery, so one can assume the author uses jQuery
Even with the jQuery, that's not what you'd do.
No, but the votes were given for something entirely different. You'd have been better off posting a new answer.
|
0

You should just use the first parameter as the element.

HTML

<li class="circle" onClick='clickCircle(this)'></li>

JS

clickCircle=function(element)
{
console.log(element)
console.log(element.tagName) // This will show LI
}

JSFiddle: http://jsfiddle.net/edi9999/WhVLm/

1 Comment

Probably even? No. (this) refers to the element as an object, (event) refers to the event itself. this.value or this.indexOf() would have worked.
0

you can do it with jquery like this:

$(function () {
  $('li.circle').on('click', function () {
    clickCircle($(this));
  });
});

var clickCircle = function (param) {
  // your code here
  // param.css('color', 'red').html();
};

or with raw javascript:

HTML

<li id="circle" class="circle"></li>

JAVASCRIPT

var circle = document.getElementById('circle');

circle.addEventListener('click', function (e) {
  clickCircle(e.target);
});

var clickCircle = function (param) {
  // your code here
  // alert(param.nodeName.toLowerCase());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.