Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically add a style for text-align using jQuery

I'm trying to correct the usual IE bugs around CSS 2.1 and need a way to alter an elements style properties to add a custom text-align style.

Currently in jQuery you can do something like

$(this).width() or $(this).height()  

but I can't seem to find a good way to alter the text-align with this same approach.

The item already has a class and I set the text-align in that class with no luck. Is it possible to just add a text-align CSS attribute to this element after a class is defined?

I have something like this

$(this).css("text-align", "center");  

just after my width adjustment and after I view this in firebug I see only "width" is the only property set on the style. Any help?

EDIT:

Whoa - big response to this question! A bit more detail around the problem at hand:

I'm tweaking the js source for jqGrid A3.5 to do some custom sub grid work and the actual JS I'm tweaking is shown below (sorry for using "this" in my examples above, but I wanted to keep this simple for brevity)

var subGridJson = function(sjxml, sbid) {     var tbl, trdiv, tddiv, result = "", i, cur, sgmap,     dummy = document.createElement("table");     tbl = document.createElement("tbody");     $(dummy).attr({ cellSpacing: "0", cellPadding: "0", border: "0" });     trdiv = document.createElement("tr");     for (i = 0; i < ts.p.subGridModel[0].name.length; i++) {         tddiv = document.createElement("th");         tddiv.className = "ui-state-default ui-th-column";         $(tddiv).html(ts.p.subGridModel[0].name[i]);         $(tddiv).width(ts.p.subGridModel[0].width[i]);         trdiv.appendChild(tddiv);     }     tbl.appendChild(trdiv); } 

I've tried both of the below (from the answers provided) with no luck.

$(tddiv).width(ts.p.subGridModel[0].width[i]).attr('style', 'text-align: center'); 

AND

$(tddiv).width(ts.p.subGridModel[0].width[i]).css('text-align', 'center'); 

I will continue to work on this issue today and post a final solution for anyone feeling my pain around this strange issue.

like image 862
Toran Billups Avatar asked May 19 '09 14:05

Toran Billups


People also ask

Which function in jQuery can be used to change the style of an element dynamically?

The css() method is used to change style property of the selected element.

Can jQuery manipulate CSS?

The jQuery CSS methods allow you to manipulate CSS class or style properties of DOM elements. Use the selector to get the reference of an element(s) and then call jQuery css methods to edit it. Important DOM manipulation methods: css(), addClass(), hasClass(), removeClass(), toggleClass() etc.

Which jQuery method is used to style an element?

css() method: The css() method is used to change the style property of the selected element. The css() in JQuery can be used in different ways.

Which jQuery action is used to add the style attribute?

css() as a setter, jQuery modifies the element's style property. For example, $( "#mydiv" ).


2 Answers

You have the right idea, as documentation shows:

http://docs.jquery.com/CSS/css#namevalue

Are you sure you're correctly identify this with class or id?

For example, if your class is myElementClass

$('.myElementClass').css('text-align','center'); 

Also, I haven't worked with Firebug in a while, but are you looking at the dom and not the html? Your source isn't changed by javascript, but the dom is. Look in the dom tab and see if the change was applied.

like image 108
Tim Hoolihan Avatar answered Oct 04 '22 14:10

Tim Hoolihan


You could also try the following to add an inline style to the element:

$(this).attr('style', 'text-align: center'); 

This should make sure that other styling rules aren't overriding what you thought would work. I believe inline styles usually get precedence.

EDIT: Also, another tool that may help you is Jash (http://www.billyreisinger.com/jash/). It gives you a javascript command prompt so you can ensure you easily test javascript statements and make sure you're selecting the right element, etc.

like image 41
kbosak Avatar answered Oct 04 '22 14:10

kbosak