0

I want to know if we can select an element by using its id and class at the same time. I know in css we can do that with #x.y, but but how can it be accomplished in javascript? I tried the following code and it worked fine but then all the controls with ui-slider-handle class got affected(which is obvious). I think I need a combination of id and class both so that only that particular element will be affected.

Javascript:

$(".ui-slider-handle").text(ui.value);
1
  • 4
    There's no point in doing that. Because "id" values have to be unique, if you know the "id" then that's all you need to know. Commented Aug 3, 2012 at 13:35

4 Answers 4

5

A combination of ID and class for selecting elements is useless as IDs are meant to be unique.

never have multiple identifiers with the same value in one page!

If you want multiple elements with the same attributes, use a class. If not, consider an ID or a class.

If you want to have a lot of elements with the same attributes, but one with extra attributes, you can give that one an ID and assign extra attributes to the ID

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

1 Comment

It's not useless if you're including a single JavaScript file in more than one HTML page.
1

You will never need to do this since the ID is unique; if you know it, you can already identify the element.

Your problem is actually that your selector matches too many elements. There are other ways to limit the "range" of a selector:

  1. Add a parent element with a certain ID/class: .parent .ui-slider-handle matches only elements with the class ui-slider-handle that are children of all elements with the class parent

  2. You can also limit by parent type: div .ui-slider-handle

  3. Or only direct children: div > .ui-slider-handle

See jQuery selectors for all the goodies.

Comments

1

Since ids should be unique, you should be able to do your selector by only id. If are wanting to apply the same attribute to multiple elements, then you should use a class. In your scenario it seems you should be fine with just using id like this:

$("#id").text(ui.value);

2 Comments

well, the issue is with slider handles where i want to show the slider value on them.All handle use this class "ui-slider-handle" and if i select that then all of the sliders will be modified..
Please be more specific with that the issue is. Is the id a container div and you are wanting to get the object that has the class which is within it? We may need to see your html to be of more help.
0

What you can write is:

$("#ID.ui-slider-handle").text(ui.value);

The string inside the quotes is a normal CSS selector, which supports both classes and ids. However, the above code is redundant and slow, and unless you want to select that particular id only if it has a certain class, it would be preferable to write:

$("#ID").text(ui.value);

Comments

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.