Solved

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

Posted on 2016-10-18
4
39 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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

12 Experts available now in Live!

Get 1:1 Help Now