0

Let's say that I have two HTML elements: element1 and element2. They are created dynamically, so I can't use IDs:

<element1 class='class1' selected="true" ...[other_attributes]...>
<element1 class='class1' selected="true" ...[other_attributes]...>
<element1 class='class1' selected="false"...[other_attributes]...>
.................................................................
{other <element1> elements}
.................................................................
<element2 class='class2' selected="true" ...[other_attributes]...>
<element2 class='class2' selected="false"...[other_attributes]...>
.................................................................
{other <element2> elements}

Now, what I want to do, is to use the same attribute (selected) for element1 and element2, and when selected="true" on element1 to have one css, and when selected="true" on element2 to have different css. Maybe something like:

[selected=true]
{
  color.red
}

[selected = true]
{
    color.green
}

Except, I want to make it in a way that the HTML will recognize what css to assign on the different element.

I hope I was clear enough.

How do I do this?

3 Answers 3

2

If it's the same element you're using, this is what classes and IDs are for:

<element id="ID1" class="red" selected="true">
<element id="ID2" class="green" selected="true">

Using IDs:

element#ID1[selected=true]
{
    color:red;
}

element#ID2[selected=true]
{
    color:green;
}

Using classes:

element.red[selected=true]
{
    color:red;
}

element.green[selected=true]
{
    color:green;
}

Note that IDs must be unique, whereas classes can be used on as many elements as you like.

Edit: Based on your modified question:

element.class1[selected=true]
{
    color:red;
}

element.class2[selected=true]
{
    color:green;
}

Alternatively you could base it on their parents (if they're different):

parent#ID1 element[selected=true] { ... }
parent#ID2 element[selected=true] { ... }
Sign up to request clarification or add additional context in comments.

3 Comments

I can't use IDs since the elements are created dynamically (I edited my post). Tnx for the help anyway!
In your edited question your element1 and element2 have different classes. Simply use element.class1[selected=true] and element.class2[selected=true].
Based on the update by the OP, your answer is definitely the way to go since the classes are already in place.
0

use this:

element1[selected="true"]
              {
  color:red
}

element2[selected="true"]
             {
color:green
}

Comments

0

Edited since you updated your example code, I've kept my original answer below for reference

Mix the class and attribute selectors like so:

HTML

<input class="class1" name="input" />
<input class="class1" />
<input class="class1" name="input" />

<input class="class2" name="input" />
<input class="class2" />

CSS

.class1[name=input]
{
    border: 1px solid red;
}

.class2[name=input]
{
    border: 1px solid green;
}

Original Answer

In CSS3 you have the :nth-of-type(n) selector, used like so:

HTML

<input name="test" value="First element">

<input name="test" value="Second element">

(Note that the example markup above is horrible and should never be done in real life!)

CSS

<style>

    [name=test]:nth-of-type(1)
    {
        color: red;
    }

    [name=test]:nth-of-type(2)
    {
        color: green;
    }

</style>

More info at http://www.w3schools.com/cssref/sel_nth-of-type.asp

2 Comments

This won't work for me, because this is for same element types but different values. I edited my post. Tnx for the help anyway!
No worries, updated my answer but its pretty much the same as one already posted it seems :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.