Results 1 to 9 of 9

Thread: CSS ID naming problem

  1. #1
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default CSS ID naming problem

    Hi,

    I'm working on a dynamically driven site with 2 links:

    Code:

    <a href="link1.html" id="Link Mine">Link 1</a>
    <a href="link2.html" id="Link Yours">Link 2</a>



    I don't have access to change the IDs on the links (get rid of the spacing in the ID names). Is there any way of styling the 2 links differently in CSS even though there is that space in the ID names?

    Thanks in advance

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You can try to access a element on CSS:
    Code:
    a{font-weight:bold;text-decoration:none;}
    But that would be applied on all the <a> elements in your page.

    If you wish to style only the two links, then you might give it a go to use JS instead:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    for(var i=0;i<2;i++){
    document.getElementsByTagName('a')[i].style.fontWeight='bold';
    document.getElementsByTagName('a')[i].style.textDecoration='none';
    }
    }
    </script>
    This changes the font-weight to bold and removes the underline of the first 2 links on your page.

    Hope it helps.
    Last edited by rangana; 07-30-2008 at 09:29 AM. Reason: Wrong Code
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    The problem is there are loads of other links on the page and I think the javascript would affect them and I don't want to style all <a> tags, just those 2 with the IDs on them. Is it possible to do anything with those IDs in CSS?

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    I have this fear that CSS can't do it. Whitespace is used as selectors in CSS. There could be two options:
    1. Find a way so that the id's don't have spaces
    2. Use JS to get your goal

    For the last option, try this script instead:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    for(var i=0,a=document.getElementsByTagName('a');i<a.length;i++){
    if(a[i].getAttribute('id').indexOf(' ')!=-1){
    a[i].style.fontWeight='bold';
    a[i].style.textDecoration='none';}}}
    </script>
    This will change the style of the <a> element that has an ID attribute with space on it.

    Hope it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the following code

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<title>Untitled Document</title>
    		<style type="text/css">
    		.special{
    			font-weight:bold;
    			text-decoration:none;
    			color:red;
    		}
    		</style>
    		<script type="text/javascript">
    		function applyCSSClass(){
    			var elIds = ["Link Yours","Link Mine"]; //You want to change the style of elements with these IDs.
    			var clsName = "special"; //Specify the CSS class name which needs to be applied on the selected elements.
    			for(var i = 0; i < elIds.length; i++){
    				document.getElementById(elIds[i]).className = clsName; //Apply the CSS class on the selected elements.
    			}
    		}
    		window.onload = function(){
    			applyCSSClass(); //After the DOM load invoke the function that applies the CSS style on selected elements.		
    		}
    		</script>
    	</head>
    	<body>
    		<a href="http://www.yahoo.com" id="Link Yours">Yahoo!</a><br/>
    		<a href="http://www.Clusty.com" id="c1">Clusty</a><br/>
    		<a href="http://www.dynamicdrive.com" id="c2">DD</a><br/>
    		<a href="http://www.google.com" id="Link Mine">Google</a><br/>
    		<a href="http://ejohn.org/" id="c3">John Resig</a>
    	</body>
    </html>
    The idea is something like this you store the element IDs which you want to apply a new style. In the above code I've created a CSS class and applied that class on the needed elements. So whatever style you want to can be put into that class.

    I have highlighted all the important section in the code.

    If you open this page in browser only the first and the fourth link will have a red color, no underlining and bold in nature. Rest of the links will be skipped.

    You can use the same principle in your case also.

    Hope this helps.

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Quote Originally Posted by rangana View Post
    I have this fear that CSS can't do it. Whitespace is used as selectors in CSS. There could be two options:
    1. Find a way so that the id's don't have spaces
    2. Use JS to get your goal

    For the last option, try this script instead:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    for(var i=0,a=document.getElementsByTagName('a');i<a.length;i++){
    if(a[i].getAttribute('id').indexOf(' ')!=-1){
    a[i].style.fontWeight='bold';
    a[i].style.textDecoration='none';}}}
    </script>
    This will change the style of the <a> element that has an ID attribute with space on it.

    Hope it helps.
    This will change all those anchor elements with an ID that contains a space character na.

  7. #7
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    This will change all those anchor elements with an ID that contains a space character na.
    Yes. Indeed the purpose of the script given.
    Apply a style to the element whose ID has a space on it (it was intended).

    The code you've provided could be a way to go too. A proof that there are multiple ways to skin a cat
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  8. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Quote Originally Posted by rangana View Post
    Yes. Indeed the purpose of the script given.
    Apply a style to the element whose ID has a space on it (it was intended).

    The code you've provided could be a way to go too. A proof that there are multiple ways to skin a cat
    The point here is if one identifies the element IDs on which he/she wants to apply the styles then what is the point in scanning through all the anchor elements unnecessarily especially he mentioned that his page has large number of anchor tags in it.

  9. #9
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    My script was a response from the OP's aim:
    Quote Originally Posted by denhamd2 View Post
    I don't have access to change the IDs on the links (get rid of the spacing in the ID names). Is there any way of styling the 2 links differently in CSS even though there is that space in the ID names?
    From above highlighted, I've written a script to scan through all the anchor tags that has an id attribute with the space in its value. On my end, I thought that the anchor elements s/he want to set a style are those that cannot be styled via CSS, and that is having space on it.

    These might be "unnecessary", but that's my thought, I could be at mistake (nothing usual) and at any point, it's up for the OP to decide which one s/he think might suit his/her needs.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •