views:

172

answers:

5

I'm trying to convert all my old javascripts to jquery but I'm a bit confused how can I convert my click event with attr. The old javascript looks something like:

function MyFunction(string1, string2){
    doSomething(string1);
    doSomethingElse(string2);
}

And then I call it with

<a href="javascript:MyFunction('string 1','string 2');">CLICK HERE</a>

Can I do this with the jquery click()? Or how can I send the two strings with an event in jquery? I need to define the two strings in the link like I'm doing it now because the strings will vary.

Edit: I have removed my jquery-code-try as it only confused :)

+1  A: 
$('#myLink').click(function() {
    doSomething('string 1');
    doSomethingElse('string 2');
});

Is that good enough for you?

Naeem Sarfraz
No :) My problem is that I don't know how I define the two attributes in jquery.As you can see Im defining them the old way in the call to the function:<a href="javascript:MyFunction('string 1','string 2');">CLICK HERE</a>
Jens
How is your anchor tag generated?
Andrew
Btw thanks for the quick reply.Right now: <a href="javascript:MyFunction('string 1','string 2');">CLICK HERE</a>But I dont know how to do it jquery way
Jens
string1 and string2 is dynamically created and will vary so I cant just define them in the script but has to send them together with the event
Jens
A: 

Naeem's solution is good, and it's hard to tell the conditions of that link. Is it dynamically created? If so you can set attributes, like

<div id="myLink" a="string1" b="string2">CLICK HERE</div>

and then

$('#myLink').click(function() {
    doSomething( $(this).attr('a') );
    doSomethingElse( $(this).attr('b') );
});
kb
Sorry that I didn't explained it well enough. I just tried to simplify it as much as possible. I need to call a function onclick with two args various places on a site. The args has to be defined in the link/div. Your solution will work but the code is not valid!I'm not sure if it is possible with click event in jqery
Jens
sorry to hear that, what kind of error do you get? it looks right to me. try putting alert() in the click-function to see if it gets called during the click event or not. also try putting alert($(this).attr('a')) to see that it gets the value correctly.
kb
No script error. It works :) But the code isnt w3 valid. I can't just add an attribute a or b on a <div> tag, and nobody of the valid attributes on <div> will make sense to use
Jens
oh, yeah, heh, sorry for misunderstanding. i guess you could place the values in hidden spans or something, but that's a little ugly. <span class="a">string1</span> and then do doSomething( $("#myLink span.a").html() ); if you really have to put the arguments in the html tag.
kb
<div id="myLink"><span class="a">string1</span><span class="b">string2</span>CLICK HERE</div> was what i meant if it was messy, and #myLink span { display: none; } in the css
kb
Hi again. No worries, just happy that you're trying to help me :) But as you wrote, this solution is ugly :(Is it not possible to call a function on click with N args in jquery like "normal" javascript: javascript:MyFunction(arg 1,arg 2 ... arg N);?
Jens
i can't think of another way to supply arguments (except different kinds of hidden elements), but i think it's difficult to remain w3 valid but still try to put the arguments in the element tag without using pseudo-protocol javascript: in some way, and when you do you really have no need for jquery at that specific point. is changing how the <div> is generated out of the question?
kb
What do you have in mind? With the last question?How would you generate the div?
Jens
+1  A: 

If the Naeem's answer is not good enough:

$('#myLink').click(function() {
    MyFunction('string 1', 'string 2');
});

Though I would like to say his accomplishes the same thing without calling MyFunction.

EDIT:

After reading your comments I think this is maybe more of what you're looking for:

$('.myLink').click(function(){
    var $this = $(this);
    MyFunction($this.attr('alt'), $this.attr('title'));
});

This will look for all element with a class of "myLink". When any of elements is clicked, MyFunction() will be called with the element's "alt" attribute as the first parameter and the element's "title" attribute as the second element.

You could of course call MyFunction() with any any of the attributes of the element you wanted by changing the parameter given to $this.attr().

smack0007
Thanks for the quick reply, and sorry that I didn't explained it well enough. I just tried to simplify it as much as possible. I need to call a function onclick with two args various places on a site. The args has to be defined in the link/div.So Naeem's and your solution wont work
Jens
A: 

There is the optional data object which is available through the event.data property on the event object passed to event handlers (I've laid the code out so it all fits without needing to scroll)

$('#myLink').bind("click", 
                  {string1:"string1", string2:"string2"}, 
                  function(event) {

                      doSomething(event.data.string1);
                      doSomethingElse(event.data.string2); 
});

<div id="myLink">CLICK HERE</div>

I'm not quite sure if this really solves the underlying problem though. Will each <a> element have different string1 and string2 values? If so, you might think about putting them in $.cache using jQuery's $.data() function, or perhaps store them in an object (which I guess is kind of the same as what you get with the cache).

Russ Cam
Hi Russ. Thanks for your reply. Yes the strings will vary so I have to call/define them from the anchor/div/click.Your solution sounds a little big compared to the job. Really confused that it has to be so difficult. The "normal" javascript is so simple: <a href="javascript:MyFunction(arg 1,arg 2 ... arg N);">text</a>
Jens
Well, you can define them as attributes on the element as kb suggested but that will break XHTML validation. The fundamental idea behind modern client-side JavaScript development is to strive for Unobtrusive JavaScript. In situations like the one you have here though, where you need to pass values which aren't directly associated with the attributes of an element, this can be difficult to achieve. Without having a clean or valid way of having those attributes stored on the element, the next logical choice is to have a store where those values can be stored and retrieved. This is the `$.cache`
Russ Cam
in jQuery and is where the event handler functions you bind to elements are stored. Putting other data in here is not difficult to do, but is a little tricky with ASP.NET web forms, but possible.
Russ Cam
And by $.cache you mean jcache? The jquery plugin?
Jens
No I mean the $.cache. When an item is stored for an element using `$.data()` (which happens when you define an event handler in jQuery), a cache property containing an object is setup on the jQuery function object. If you have firefox and firebug, open the console on this page, type in `$.cache` and click run. You'll see the object contained in the cache property in the left pane. Click on it to explore it. It has numerical properties, each numerical property corresponding to a unique id assigned to an element when it stores a value. The unique id is stored on the element in ...
Russ Cam
a property named `jQuerytime` where time is the number of milliseconds since 1 Jan 1970 at the point at which the script executed.
Russ Cam
A: 

Thanks everybody for your help. I ended up using kb's solution.

<a href="#" class="myLink" title="string1" rel="string2">click me</a>

And

$('.myLink').click(function(){
   var $this = $(this);
   alert('Output: ' + $this.attr('title') + " and " + $this.attr('rel'));
});
Jens