SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementId change color of text

    Hi site point people,

    I have been playing with javascript and I have one issue I can't workout. below in my code on a click the td will change
    its background image and text color. The issue I have I can't get the color to work but the image is find. Any help would be
    great thanks

    Code:
    <td>
             <a href="javascript:cont(specialId)" class="position-one"><span>water</span></a>
    </td>
    Code:
    function cont(specialId){
    document.getElementById(specialId).style.backgroundImage = "url(select-buttons/selected.png)";
    document.getElementById(specialId).style.color = "orange";
    }

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few points:

    1)

    Code:
    <a href="javascript:cont(specialId)" class="position-one"><span>water</span></a
    is bad. href is meant to be used to navigate to another page, not run javascript.

    A better way is

    Code:
    <a href="" onclick="cont('specialId'); return false;" class="position-one"><span>water</span></a>

    2) where have you defined specialId in javascript:cont(specialId)?

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can do it this way:

    HTML Code:
    <a href="#" id="specialId" class="position-one"><span>water</span></a>
    Code:
    var spcID = document.getElementById('specialId');
    	spcID.onclick = function(e) {
    		e.preventDefault();
    		this.style.backgroundImage = "url(select-buttons/selected.png)";
    		this.style.color = "orange";
    	};

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks I have made the changes I have the special id in the the td it is dynamically written by the php.

    <td id="specialId"> // sorry left that bit out

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok , then you would also need to wrap the argument in your call to cont in quotes.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help


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
  •