Solved

How to force set he height of an element using jquery

Posted on 2016-10-02
5
51 Views
Last Modified: 2016-10-14
HI,
I am using the following code to set the height of the element in jquery :
                   $('.cke_contents').css('height', middleHeight - toolbarHeight);

But the issue is there is already a css rule which sets height to some 100px !important because of this important thing my changes to height using .css function are not getting applied. So one way out of this i think is somehow add !important to the .css function ??

Or there is any other alternative way ?

Thanks
0
Comment
Question by:Rohit Bajaj
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 350 total points
ID: 41825478
You would need to use the "cssText" argument for this instead of altering height directly. It will look like this:
$('.cke_contents').css('cssText', "height: ' + (middleHeight - toolbarHeight) + 'px !important;');

Open in new window

0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41825688
What are the values for middleHeight and toolbarHeight. If these are string values with unit indicators, the calculation would generate an unexpected result. Have you checked that the calculation is working properly?
console.log('Calculated height = ' + (middleHeight - toobarHeight) );

Open in new window

0
 

Author Comment

by:Rohit Bajaj
ID: 41831404
Hi,
the values are just numbers like 300 or 100.
Also i read up that cssText clears everything else... so if any other styling info was there on the element it will clear that up..whereas in my case i only want to adjust the height
0
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 150 total points
ID: 41832650
Are you including a unit indicator as in Russ Suter's suggested code (px)?

cssText replaces the value of the style property of the element. It has no impact on the styles applied by embedded or remote stylesheets unless it directly overrides a specific property. But it will replace any previously defined inline styles. If you have other inline styles you wish to retain, stick with the .css('height',...) method. But be sure to include a unit designation.
 $('.cke_contents').css('height', (middleHeight - toolbarHeight)+'px');

Open in new window

1
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41832792
Indeed, my test confirms that what Kim posted should work.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

776 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