Solved

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

Posted on 2016-10-18
4
32 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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Use CSS text-align.
0
 

Author Closing Comment

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

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

Expert Comment

by:Julian Hansen
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now