3

I'm trying to assign an ID to a DIV that already has a Class attached to it. Here's my code: (The class is "myclass" and the id is "myid")

$(document).ready(function(){
$(".myclass").id("myid");
 });

Thanks in advance.

4
  • 1
    This is jQuery, not pure JavaScript please tag properly in the future. Commented Dec 18, 2011 at 15:35
  • Is there more than one element with that class? Commented Dec 18, 2011 at 15:36
  • Thanks @ShadowWizard, will do in the future. Commented Dec 18, 2011 at 15:37
  • @RightSaidFred Nope, just the one class that I'm trying to add an ID to. Commented Dec 18, 2011 at 15:37

3 Answers 3

4

Try with .prop() if you use jQuery 1.6+ or .attr() otherwise:

$(".myclass").prop("id", "myid");

If you have more than one element with that class, it will assign same ID to multiple elements which is very bad practice.. in this case append the index:

$(".myclass").each(function(index) {
    $(this).prop("id", "myid" + index);
});

Edit:

Most elegant and efficient way is using .prop() or .attr() (in pre 1.6) directly without .each() then assigning the id of the direct DOM element:

$(".myclass").prop("id", function(index) {
    return "myid" + index;
});

Live test case.

Sign up to request clarification or add additional context in comments.

10 Comments

@Tom true, but what if he got more than one?
+1 for .prop(), but in the .each(), I'd suggest skipping the method and using the property directly.
The point was the clarification of (don't mess with same ID for bunch of elements). You've correctly edited your comment hehehe. @RightSaidFred you can't use it directly, because you'll have more than 1 .myclass in some time.
.attr and .prop can take functions as arguments to automatically loop .each for you, so you don't need the separate .each.
@TomRoggero: Why would that make a difference in the .each loop? You'd just change the code to this.id = "myid" + index.
|
2

It's

$(document).ready(function(){
$(".myclass").eq(0).attr("id", "myid");
});

I wrote eq(0) because there cannot be two DOM elements with same ID, but there could be more than one .myclass

2 Comments

Why not: $(".myclass")[0].id = "myid";? Several less function calls and one less jQuery object.
@jfriend00 it will crash with error when there's no element with that class.
1

you can use .attr()

$(document).ready(function(){
$(".myclass").attr("id","myid");
 });

fiddle : http://jsfiddle.net/LFQeR/

3 Comments

This could set multiple selements all to the same id.
In the question its not made clear if there are one or multiple div with same class and he wants to set the id just to one div or all divs
"Is there more than one element with that class? Nope,just the one class that I'm trying to add an ID to" - this is the comment posted by the question raiser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.