dannyg280
asked on
After replacing cell text using JavaScript, text is no longer centered
I have a table that contains a cell like this:
The "2406.31" is displayed centered in the cell.
I have the following JS which changes that value when a drop down option in the same row is selected:
The script works fine but when the cell text is replaced with the new number is is aligned left instead of centered. Is there a way for me to keep the cell text centered after being replaced?
<td class="catop"><center>2406.31</td>
The "2406.31" is displayed centered in the cell.
I have the following JS which changes that value when a drop down option in the same row is selected:
$(".rank").change(function() {
var rankop = this.value; // get Rank value
var $catrow = $(this).closest("tr"); // Find the row
var $catop1 = $catrow.find(".catop").text(); // Find cat Op
var $catop = (rankop * $catop1); // Multiply ranking x op for new op
$catop =$catop.toFixed(2); // format to 2 decimal points
$catrow.find(".catop").text($catop); // change text of catop cell to new op
});
The script works fine but when the cell text is replaced with the new number is is aligned left instead of centered. Is there a way for me to keep the cell text centered after being replaced?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use CSS text-align.
ASKER
Thank you. I went with CSS to keep all the headers and cells centered.
td,th {
text-align: center;
}
td,th {
text-align: center;
}
1. <center> has been deprecated in HTML5
2. your code does not have a closing </center>
CSS
2. your code does not have a closing </center>
<td class="catop"><center>2406.31</td>
3. There are two ways of doing this<td class="catop"><center><span>2406.31</span></center></td>
Target the <span> instead of the <td>. I provide this for interest - in case you have a similar situation. In this case the correct method is the one suggested by Ste5an [EDIT: and Chris] - use CSSCSS
...
td.catop {
text-align: center;
}
...
HTML...
<td>2406.31</td>
...