2 Replies - 3607 Views - Last Post: 21 September 2012 - 08:43 PM

#1 cmbasse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 12

Changing the style of td tag

Posted 20 September 2012 - 07:41 PM

I have a table created via php like this:
    <table>
  <?php
  $squery = "SELECT DISTINCT(Year_Played) AS yp, Sessions  FROM pinkpanther_games ORDER BY id DESC";
  $sresults = mysql_query($squery);
   while ($srow = mysql_fetch_assoc($sresults)){
 echo "<tr><td>";
 echo "<a href='#' onclick=\"\">Session " . $srow['Sessions'] . "<br>" . substr($srow['yp'], 0, 4) . "-". substr($srow['yp'], 4, 7) . "</a>";
 echo "</td></tr>"; 
   }
  ?>
    </table>   



and then i have this function:
 <script type="text/javascript"> 
 $("#spicker td").bind("click", function(){
 $(this).attr("class", "special");
});
</script>




and it doesn't work. I have tried this function on a table not created with php and it works fine so i don't know what is wrong

this is the style I'm trying to change to:
.special{
 width: 150px;
 padding-left:25px;
 padding-top: 25px;
 padding-bottom: 25px;
 background-color: #fff;
 color: #000;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Changing the style of td tag

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Changing the style of td tag

Posted 20 September 2012 - 09:13 PM

The method you are using replaces all existing classes on that selector with the class special. This may not be what you want (I'm not sure what other classes that selector has inherited.
Have you tried the methods .addClass(} and .removeClass? There is also .toggleClass(), which could be useful depending on your requirements
Was This Post Helpful? 1
  • +
  • -

#3 cmbasse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 12

Re: Changing the style of td tag

Posted 21 September 2012 - 08:43 PM

I modified some styles and then did this and now it all works great
 $("#spicker td").bind("click", function(){
	$('#spicker td').each(function(index, element) {
	    if ($(this).hasClass("special")){
			$(this).toggleClass("special");	
		}
    });
	$(this).toggleClass("special");
	
});


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1