5
Contributors
22
Replies
68
Views
3 Years
Discussion Span
Last Post by Siberian
Featured Replies
0

The original script has an onClick for an anchor, I want a function for each event.

$('anchor','anchor2','anchor3').onClick(function...)

There are three events in the above script, depending on what event is clicked of the three a different HTML page would load. Instead of creating a event for each anchor.

0
$('#anchor, #anchor2, #anchor3').click(function(){ ... });

When you say multiple events - do you mean one method, but multiple targets?

$('#anchor, #anchor2, #anchor3').click(function(){ ... }).hide('fast');

That's an example of chaining methods

Edited by diafol

0

Do you mean multiple events and multiple targets, each event does does almost the same thing, except load a different DIV. I assume you can't have jQuery load different content within the same DIV (HTML/CSS etc) you need multiple DIV's, correct ?

1

WTF? Please articulate your needs clearly, you're not making any sense.

Edited by diafol

0

Maybe something like this:

$('#anchor, #anchor2, #anchor3')
    .click(function(){ ... })
    .dblclick(function(){ ... })
    .hover(function(){ ... });

Edited by AndrisP

0

Thanks AndrisP that is what I meant.

I have one question, does it work in the order of the chain, for example #anchor1 is associated with .click, #anchor2 is associated with .dbleclick etc ?

0

I have one question, does it work in the order of the chain, for example #anchor1 is associated with .click, #anchor2 is associated with .dbleclick etc ?

No

Thanks AndrisP that is what I meant.

I thought I'd already given that example...

$('#anchor, #anchor2, #anchor3').click(function(){ ... }).hide('fast');

Oh well.

Consider using data-* attributes for keeping info if you want to alter the result of a method if you're using multiple targets.

<p id="anchor" data-id="4">...</p>

Edited by diafol

0

The data attribute may be just what I want :) Thanks. Sorry for the confusion regarding this question !

0

Thanks. Sorry for the confusion regarding this question !

heh heh - don't worry, I'm used to it :)

0

I never knew about the data- attribute in HTML5. How would I use this in jQuery if I had a chain of anchors and each anchor should load a different DIV within the same page ?

Most importantly, can jQuery change the content on a DIV or must I have multiple DIV for the page each DIV representeting the different content I want to show which is linked with the each anchor in the chain of anchors in the jQuery ? For example $("anchorA","anchorB").click() anchorA is linked with a DIV on the page called "ContentA", anchorB is linked with a DIV on the page called "ContentB" or is jQuery powerful enough to literally change the content on the DIV so each anchor loads the same div on the page, but is smart enough to know that a different anchor was clicked in the chain and so the content must be changed before doing whatever the script does, that means, whatever presentational things the script has etc ?

0

Instead of doing this ... $("anchorA","anchorB").click(), assign both divs the same class. Then just use the class name as the selector.

$(".anchor-class").click()

Then to reference which element was clicked you use: $(this)

That's how jQuery knows which one was clicked.

0

diafol mentioned the data attribute, how could this be used instead of classes, or is it almost identical ?

0

The data attribute can be used as a selctor as well. There are many ways to select your elements. Its up to you on which seletor works best.

You should read up on selectors so you have a better understanding about them.

But with regards to the data attribute, it's better used to store information that isnt necessarily to be used to display on the screen. Think of the data attribute a vehicle for you to be able to create your own custom attributes and store values in those attributes, almost like a variable.

0

Data attribute is as you said another way to do things. I could get the ElementByID, then once I have that, get the dataset or I could apply a class. Thanks :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.