0

I'm trying to change css using jQuery but mso-hide property doesn't get saved using jQuery.

What other solution will be to do this using jQuery?

styleCSS = {
    'display': 'none',
    'height' : '0px',
    'line-height' : '0px',
    'max-height' : '0px',
    'overflow' : 'hidden',
    'font-size': '0px',
    'mso-hide' : 'all',
    'width' : '0px',
    'max-width' : '0px'
}
$('.container').find('.hide-me').css(styleCSS);
6
  • Have you tried directly implementing the style without using find? Commented Dec 20, 2016 at 9:27
  • 1
    Possible duplicate of Can I fetch the value of a non-standard CSS property via Javascript? Commented Dec 20, 2016 at 9:31
  • yes. the same happens Commented Dec 20, 2016 at 9:31
  • @AlexandruRada as you can se in my answer live snippet working properly Commented Dec 20, 2016 at 9:35
  • 2
    use a class like normal people do place your css in a css file Commented Dec 20, 2016 at 9:46

2 Answers 2

1

You can set whole style attribute manually

var style_text = '';
$.each(styleCSS, function(k,v) {style_text += k+':'+v+';';});
$('.container').find('.hide-me').attr('style', style_text);

styleCSS = {
    'display': 'none',
    'height' : '0px',
    'line-height' : '0px',
    'max-height' : '0px',
    'overflow' : 'hidden',
    'font-size': '0px',
    'mso-hide' : 'all',
    'width' : '0px',
    'max-width' : '0px'
};

var style_text = '';
$.each(styleCSS, function(k,v) {style_text += k+':'+v+';';});
$('.container').find('.hide-me').attr('style', style_text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="hide-me">foo</div>
  <div>bar</div>
</div>

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

1 Comment

used a variation of this. Didn't think about using style.
0

What other solution will be to do this using jQuery?

An alternative approach would be to declare var styleCSS as a string variable (instead of an object) and then simply append an additional <style> element to the <head> of the document via jQuery:

$(document).ready(function(){

var styleCSS = '.hide-me {display: none; height: 0; line-height: 0; max-height: 0; overflow: hidden; font-size: 0; mso-hide: all; width: 0; max-width: 0;}';

$('head').append('<style>' + styleCSS + '</style>');

});

Comments

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.