views:

137

answers:

4

Ok here is what I am trying to do.

I have a few effects I want to apply to several different div containers. Lets say I have a slide effect and a fadeTo effect. Rather then specifying what the div class/id is in the JS code, I would prefer to just be able to apply the effect by just giving the div a custom tag or an additional id/class. I always want to specify the parameters of the effect on the actual div.

For instance.

<div id="cssDesign SlideEffect" [Speed, Duration parameters somewhere]></div>

or

<div Customtag="SlideEffect" id="cssDesign" [Speed, Duration parameters somewhere]></div>

Make sense?

+1  A: 

It's not valid XHTML, but you can always add non-standard attributes to html tags.

<div id="cssDesign" SlideEffect="Speed,Duration"></div>

Then you can write your selectors based on the attribute.

$("div[SlideEffect]").mouseover(function() {
    var speed = parseInt($(this).attr("SlideEffect").split(',')[0]);
    var duration = parseInt($(this).attr("SlideEffect").split(',')[1]);
});

(Read more about attribute selectors here.)

Joel Potter
If you use this approach (which I like), I suggest using a `data-` prefix on your custom attributes. That makes it future compatible with HTML5 http://ejohn.org/blog/html-5-data-attributes/
Michael Haren
That makes sense for declaring the parameters, thanks!Here is my code:$(document).ready(function() { $('#slideleft button').click(function() { var $lefty = $(this).next(); $lefty.animate({ left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0 }); });});So instead of declaring to apply a slide effect to the div class, how would declare to use the effect within the div?
Jared
It's no different, only instead of using `$(this).attr("SlideEffect")` you would use `$("#slideleft").attr("SlideEffect")`. In my code I created a mouseover event, but that was just for completeness.
Joel Potter
@Michael: That's a good suggestion, and one I should follow more myself.
Joel Potter
+2  A: 

If you want to keep your pages conforming to the XHTML standard, you can use some existing but not used attribute, such as rel to store the data. It's not semantically correct but hey, it validates.

<div rel="slide,500"></div>

And jQuery:

$('div[rel^=slide]').each(function() {
    var data = $(this).attr('rel').split(',');
    var speed = data[1];
});

And so on. Instead of rel, you can also use id if you don't need it for anything else (in most cases, you can substitute the use of id with class).

Tatu Ulmanen
Learn to love the `rel` tag. +1
cballou
+1  A: 

Using custom tags or attributes is generally not recomended (the HTML document will not pass the validation, that could affect indexing by search engines). In similar situation i used one of the proper html tags, like class or id and place parsable info there:

<div id="mydiv_slide_500">...</div>

Then you have to extract the attribute value, and parse it with split('_'). It is a more complicated than using custom tags, but you get the valid HTML.

PanJanek
A: 

Forgive my complete JS n00bish here..

I can't figure out how to plug that in here:

$(document).ready(function() {

  $('#slidemarginleft button').click(function() {
    $(this).animate({
      marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ?
        $marginLefty.outerWidth() :
        0
    });
  });
});
Jared