Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2007
    Location
    FL
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Changing Style with JS

    I have a TD that i want to change the style on a TD when i click on it.

    Code:
    <script language="javascript" type="text/javascript">
    		function showstuff(boxid){
    		      document.getElementById('td_Contact').style="new";
    		}		
    	</script>
    
    <table width="100" >
    						<tr>
    							<td class="tab_normal" id="td_Contact">
    								<a onClick="showstuff('Contact')" class="tab_nav">Contact</a>
    							</td>
    Thanks for all the help

  • #2
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    You'll need to either set each of the style attributes individually like so:
    PHP Code:
    <td class="tab_normal" id="td_Contact" onClick="this.style.backgroundColor='#FF0000';this.style.color='#FFFFFF';">
        <
    a onClick="showstuff('Contact')" class="tab_nav">Contact</a>
    </
    td
    this.style.backgroundColor='#FF0000';this.style.color='#FFFFFF'; could also be made into a javascript function (that you'd call onClick of course)... if you expect to use it multiple times.

    Or you can set the class of the table cell (if that CSS class exists of course) to change its style like so:
    PHP Code:
    var IE /*@cc_on!@*/false;
    function 
    SetClass(Class) {
        if (Class) {
            try {
                if (
    IE) {
                    
    this.setAttribute('className', Class);
                } else {
                    
    this.setAttribute('class', Class);
                }
            } catch (
    e) {
                try {
                    if (
    IE) {
                        
    this.setAttribute('className', Class);
                    } else {
                        
    this.setAttribute('class', Class);
                    }
                } catch (
    e) {
                    
    //alert(e);
                
    }
            }
        }

    Usage:
    PHP Code:
    <td class="tab_normal" id="td_Contact" onClick="SetClass('SomeClass');">
        <
    a onClick="showstuff('Contact')" class="tab_nav">Contact</a>
    </
    td
    Or if you want to keep the tab_normal class:
    PHP Code:
    <td class="tab_normal" id="td_Contact" onClick="SetClass('tab_normal SomeClass');">
        <
    a onClick="showstuff('Contact')" class="tab_nav">Contact</a>
    </
    td

    EDIT:
    You can also do this:
    PHP Code:
    <td class="tab_normal" id="td_Contact" onClick="this.setAttribute('style', 'background:#FF0000;color=#FFFFFF');">
        <
    a onClick="showstuff('Contact')" class="tab_nav">Contact</a>
    </
    td
    Last edited by tfburges; 10-02-2010 at 03:19 AM.


  •  

    Posting Permissions

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