0

I need to change the DOM dynamically. I want to access the 'text-align' and the 'line-height' properties.

for example:

<p style="line-height:1.0;text-align:justify;">

I need to access all the style attributes in the DOM and go through each 'text-align' and 'line-height' properties and change their values. How to access these properties. Actually what I need to do is go through these two properties and change the text-align to left and change the line-height to 1.5 if it's less than 1.5

Thank you so much inadvance.

2
  • 1
    Is this what you mean: $("SELECTOR").css('PROPERTY'); ? Commented Jun 1, 2014 at 15:22
  • As this style attribute can be anywhere, I need to go through all the style attributes and then should be able to access the two properties Commented Jun 1, 2014 at 15:24

4 Answers 4

2

You can use $("#selector").css({'property' : 'value'});

from the docs : http://api.jquery.com/css/

You can set properties using this method.

Edit :
to check if an element has a css style, you can use the following SO answer :
jQuery: check if element has CSS attribute

Sign up to request clarification or add additional context in comments.

Comments

2

You can use .css method to achieve this:

$('p').css({'lineHeight':'1.0','textAlign':'right'})

Update:

If you want to select all elements that has line-height style you can use following selector:

$('*[style*="line-height"]').css({'lineHeight':'1.5'})

2 Comments

since the style attribute can be anywahere (inside a <p> tag or somewhere else) in that case I cant check for all the tags. so is there anyway I can access all the style attributes ans then check for the two properties?
@user3547920 Yes, you can select all element that have line-height style like using this selector: '*[style*="line-height"]'
1

Try:

$(document).ready(function(){
$("*").each(function(){
alert($(this).css('lineHeight'));
if($(this).css('lineHeight')){
$(this).css({'lineHeight':'1.0'});
}
if($(this).css('textAlign')){
    $(this).css({'textAlign':'right'});
}
});
});

Here $("*") will select all elements in DOM and then .css("propName") will check whether the propName property exists in that element's css. and if it exists then .css({'propname':'newValue'}); will assign a new value to that property.

Hope it helps.

1 Comment

Thank you. I need to get the excisting lineheight value and check wether it's less than 1.5 if so replace it with 1.5.
1

As mentioned before, you can use:

$('p').css({'lineHeight':'1.0','textAlign':'right'});

You can also use:

$('p').css('lineHeight', '1.0');
$('p').css('textAlign', 'right');

Although not recommended, because this would replace all other styles, you could even do:

$('p').attr('style', 'lineHeight: 1.0; textAlign: right;');

To select all elements on the dom, just use the $('*') selector.

$(document).ready(function(){
    $("*").each(function(){
        // 1.0 == 16px | 1.5 == 24px
        if(parseInt($(this).css('lineHeight'), 10) < 24){
            $(this).css({'lineHeight':'1.5'});
        }
    });
});

HERE is the JSFiddle

2 Comments

Thank you. I need to get the excisting lineheight value and check wether it's less than 1.5 if so replace it with 1.5.
@user3547920 see if this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.