okenshield
okenshield

Reputation: 565

having same div twice

I have a div on a form but need it twice on the form, with both being shown/hidden independently. Is there a way using jquery plugin to have the same div twice but with the same class name?

Upvotes: 1

Views: 1996

Answers (4)

creechy
creechy

Reputation: 405

You can use a class as many times as you want in HTML. But an id, on the other hand, technically can only be used once within an HTML page.

There are many examples of selectors for specific classes, for example, $("div.myclass") and then use .each() to iterator over all of them, for example:

$("div.myclass").attr("display", "none");

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 992857

More than one <div> element may have the same class name. Also, a <div> element can have more than one class name. For example:

<div class="class1 class2">first</div>

<div class="class1 class3">second</div>

can be controlled together using the class1 name, or independently using the class2 and class3 names.

Upvotes: 2

Šime Vidas
Šime Vidas

Reputation: 185893

Here:

$( elem ).clone( true ).insertAfter( elem );

Live demo: http://jsfiddle.net/gFB7Y/

So, you clone your DIV and insert the clone after the original (or anywhere you like).

Upvotes: 1

Jasper
Jasper

Reputation: 75993

There is nothing needed, two elements with the same class will not conflict if you are selecting them by class:

$('.my-class').bind('click', function () {
    $(this)...//this always references the actual element on which the event fired
});

If you need to target the element from a function where this does not refer to the correct element you can use .eq() to select the proper index:

$('.my-class').eq(0).trigger('click');//this will only trigger a click on the first element found, you can use `.eq(1)` for the second, etc.

Upvotes: 1

Related Questions