Solved

How to force set he height of an element using jquery

Posted on 2016-10-02
5
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arrays and radio buttons 10 40
Link Anchor Not working 9 50
CSS for <center> 14 35
HTML editor custom button 3 60
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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).

726 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