Syntax problem

I’m using JQuery


<script type="text/javascript">
		$(document).ready(function()
		{
			$("#Cluster").click(function()				
			{
				$(".[COLOR="Blue"]AcasCluster[/COLOR]").each(function()
				{
					this.click() ;
					
					
				});
			});					
		});
		
</script>



This code works .

AcasCluster is a hard-coded value though. I want to make it a parameter so that I can pass the value through the function.

But how to parameterize this function ? I’m not sure about the syntax . I’m stuck at this part.
Something like below probabily …I’m not sure at this part …I need help here.


<script type="text/javascript">
		$(document).ready(function()
		{
			$("#Cluster").click(function(var [COLOR="Red"]param[/COLOR])				
			{
				$(".[COLOR="Red"]param[/COLOR]").each(function()
				{
					this.click() ;
					
					
				});
			});					
		});
		
</script>


param will hold the class values in fact. Could you please correct my syntax ?
Also, I’m skeptical as to how to pass the class value to the clicked function also ?

Yeah, if that was the purpose then your code will do what he wants. I didn’t interpret it that way, but you may well be right.

I thought he wanted to reuse the same event listener for different click events and only vary the class name of the targets.

Well, he (or she) now has a solution for both cases! :slight_smile:

Sorry, but i thought we wanted a closure, so that param could be changed and the listeners would always use the current value. Your example does not currently remove existing listeners, but would have to be called again and again, binding a different set of listeners for each parameter change.

@r51: That will work if the call exists in one place only, but then you wouldn’t need an extra parameter anyway. You are creating a closure with a reference to a variable with ‘top-level’ scope. If you modify that variable’s value later in the code, it will affect the referenced value inside the event listener.

That’s why you need to pass it as I showed in post #4, using an anonymous self-calling function.

Sorry, my post is wrong. Lose the second parameter to the event listener.


var param = "AcasCluster";

$("#Cluster").click(function(e)				
{
	$("." + param).each(function()
	{
		this.click() ;
	});
});

The above might work, depending on the context. The value of param when the listener is called will be used.

jQuery event listeners will have the event object passed to them as the first parameter. You could add an extra parameter.


var param = "AcasCluster";

$("#Cluster").click(function(e, param)				
{
	$("." + param).each(function()
	{
		this.click() ;
	});
});

As I said, I don’t use jQuery, but I assume that the .click() method adds an event listener for the click event. An event listener is called with a single argument – an Event object (except in Internet Explorer which doesn’t pass any argument; instead relying on a global window.event object).

So if you want to pass an extra parameter you’ll have to wrap the call inside a self-calling anonymous function and create what is known as a closure.

$("#Cluster").click(
    (function (param) {
        return function (e) {
            /* your normal event handler code using 'param' goes here */
        };
    })("AcasCluster")
);

I don’t use jQuery, but have you tried this?

$("." + [color=red]param[/color]).each(...)

thanks for the reply. I’ll try that way.

Could you please also tell , how do I send the value to the function from the html element?

As you see I have

$(“#Cluster”).click(function(var param)

how do I pass the value to this function from a HTML element whose ID=“Cluster” when it is clicked.

If I had javascript I could pass this way …

onclick=“click(‘avalue’)” from the HTML element

But in JQuery click function how do I pass the value to the click function from HTML element ?