tags:

views:

59

answers:

6

First I realize ID's should be unique. But right now I can't do much about that. I have a javascript plug-in that is generating ID names and for one page it works great. The issue is in creating another page, it will start over using the same naming convention. For example:

Page 1

<ul id="1">

Page 2

<ul id="1">

So if I am trying to style ul#1 on Page 1 it will also style ul#1 on Page 2. So, any suggestions on how to separate our the two id's? This html is generated by the JS, otherwise I would just attach a class to it.

Thanks.

+1  A: 

Can you attach a class around it ? Have a div or span some other element surround your code that does the generation and assign a class to it.

sjobe
+1  A: 

I'd say you have to use different style sheets on each page if you need different styles for the same ids, but this will be a pain to maintain as you make styling changes.

Alternatively you could you assign a class to one of the page's UL tags and then create a style for that class.

Abe Miessler
+1  A: 

First of all, the plugin is still not generating the correct ids because ids can't be numbers. To answer your question, try to figure out some parent element that might be different between the two pages probably in which case you can use CSS such as this:

#parent ul#1{
  /* styles here */
}

#parent2 ul#1{
  /* styles here */
}

page1:

<div id="parent">
  <ul id="1">
    ............

page2:

<div id="parent2">
  <ul id="1">
    ............

So you need to find out a some parent element of ul which is not common between the two pages. This is the only possibility that comes to my mind where you have no control over changing the ids or replacing them with classes since they are being generated by the plugin.

Sarfraz
+1  A: 

You need something to distinguish them if you want them styled separately. If you cannot modify those tag you could probably use some parent container like:

<div id="parent1">
    <ul id="id1" />
</div>

<div id="parent2">
    <ul id="id1" />
</div>

and then:

#parent1 ul {
    ...
}

#parent2 ul {
    ...
}

Also notice that an id cannot start with a number as in your case. You should probably consider switching/modifying this plugin.

Darin Dimitrov
I believe this will work for me. It is just bad practice but I don't really have a choice. Thanks.
Xtian
+1  A: 

First, the unique ID suggestion is restricted to a page. It is perfectly fine to have multiple ID's on different pages. The best way to overcome this is to add a ID to the body.

Page1

<body id="Page1">
    <ul id="1">
        <li></li>
    </ul>
</body>

Page2

<body id="Page2">
    <ul id="1">
        <li></li>
    </ul>
</body>

CSS

#Page1 #1
{
    //Some style for page 1, ID 1
}


#Page2 #1
{
    //Some style for page 2, ID 1
}
Dustin Laine
Thank you, makes perfect sense now.
Xtian
A: 

One thing I commonly do is attach a class to the body for each page. <body class="home_section"> and then you could style based on that class .home_section ul#1 {}.

Also, IDs must begin with a letter.

Jeff Adams