tags:

views:

56

answers:

2

How to add cellspacing="0" if it's not available for any <table> using jquery?

and if it's available and it has any other value than ="0" then want to convert it to ="0"

+2  A: 
$('table').attr("cellspacing", 0);

Will change every table's cellspacing attribute to 0, whether it was previously defined or not.

Starting HTML:

<table> ... </table>
<table cellspacing="5" cellpadding="5"> ... </table>

Resulting HTML:

<table cellspacing="0"> ... </table>
<table cellspacing="0" cellpadding="5"> ... </table>
Nilloc
and will it add `cellspacing="0"` to `table` if it's not available?
metal-gear-solid
Seems exactly correct. The question is weirdly asked (sorry MGS), but basically the OP wants `cellspacing="0"` on all tables, and this is the way to do it.
Kobi
A: 
 <table id="table_id" cellspacing="0px">
 .................

Try:

if ($('#table_id').attr('cellspacing')) // does attribute exit
{
  if ('0px' != $('#table_id').attr('cellspacing'))
  {
    $('#table_id').attr('cellspacing', '0px') // make its value 0
  }
}
else // attribute does not exist
{
  $('#table_id').attr('cellspacing', '10px') // add
}
Sarfraz
`0px` is wrong only `0` is correct
metal-gear-solid
@metal-gear-solid: It is not wrong, try that out if you know. `cellspacing = '0px'`, it will come out to be `0px` if you set it to `cellspacing = '0'`, it will come out to be `0` string not number still.
Sarfraz
@sAc - OK I thought we don't need measurement unit for `0` in html.
metal-gear-solid
"0" is the same as "0px" (and "0em" and "0pt" and ...) In CSS, the units are not *required* when specifying a value of "0", but you can put them in if you like.
Dean Harding
@metal-gear-solid: You should add further details in question so that the answer you like is provided. Always provide complete details in your questions please. And replacting units should not be a problem.
Sarfraz
@Dean 'codeka' Harding: That's right :)
Sarfraz
@Dean actually <code>em</code>'s won't work because this isn't a CSS value, but rather and HTML attribute. So <code>"10"</code>, <code>"10px"</code> and <code>"10pt"</code> are all the same... I think only "10" is valid however and will get you the most consitent results.The CSS equivalent is <code>border-spacing:10px</code> and should be included in something like a reset.css which is generally used to normalize display across browsers.
Nilloc