Solved

After replacing cell text using JavaScript, text is no longer centered

Posted on 2016-10-18
4
45 Views
Last Modified: 2016-10-18
I have a table that contains a cell like this:

<td class="catop"><center>2406.31</td>

Open in new window


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

});
	

Open in new window


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?
0
Comment
Question by:dannyg280
4 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 41848433
When you replace the text, you're overwriting the <center> tag, so you will need to include that in the new text. Something like this:

$catop = $('<center>').text($catop.toFixed(2));
$catrow.find(".catop").html($catop);

Open in new window


Alternatively, use CSS on the catop class
1
 
LVL 33

Expert Comment

by:ste5an
ID: 41848445
Use CSS text-align.
0
 

Author Closing Comment

by:dannyg280
ID: 41848530
Thank you. I went with CSS to keep all the headers and cells centered.

td,th {
  text-align: center;
}
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41848537
1. <center> has been deprecated in HTML5
2. your code does not have a closing </center>
<td class="catop"><center>2406.31</td>

Open in new window

3. There are two ways of doing this

<td class="catop"><center><span>2406.31</span></center></td>

Open in new window

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 CSS
CSS
...
td.catop {
  text-align: center;
}
...

Open in new window

HTML
...
<td>2406.31</td>
...

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question