Solved

Change Max-Height Property On A Div .Class Upon Click

Posted on 2008-10-10
5
528 Views
Last Modified: 2013-11-19
How to remove the max-height setting from a single div element via class attribute upon click?

I have several rows of text, each row is enclosed within a div.  For example;


<div class="promotext">Blah Blah Blah</div>

<div class="promotext">Blah Blah Blah</div>

<div class="promotext">Blah Blah Blah</div>


In the css stylesheet I have the setting

.promotext {max-height:3.7em; overflow:hidden;}

This ensures that if 'Blah blah blah' gets too long, after 3 lines, the text overflow is hidden.  However, I want to enable the functionality that if a user clicks on the 'Blah blah' text, the max-height property is removed or nullified so that the full text becomes visible - however, only the div that was clicked should expand.

So setting a new style on .promotext wont cut it because all the text rows will expand, because we only want the clicked upon div to expand.  The text rows are generated dynamically, sometimes there is 1-3 rows, sometimes there is upto 50 text rows.  I cannot assign a unique ID to each text row as it is a template which repeats itself.

Any help in creating an expanding div solution would be much appreciated.  Thanks.
0
Comment
Question by:thyros
  • 2
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 22691645
Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<style type="text/css">/* <![CDATA[ */
div.promotext {
  max-height:3.7em;
  overflow:hidden;
}
/* ]]> */</style>
<script type="text/javascript">/* <![CDATA[ */
// http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}
function promoClick(){
  this.style.maxHeight = (this.style.maxHeight?'':'none');
}
window.onload = function(){ /* quick and dirty onload event handler */
  var elms=getElementsByClassName('promotext');
  for(var i=0,l=elms.length;i<l;i++) {
    elms[i].onclick=promoClick;
  }
}
/* ]]> */</script>
</head>
<body>
 
 
<div class="promotext">Blah Blah Blah</div>
 
<div class="promotext">Blah Blah Blah</div>
 
<div class="promotext">Blah Blah Blah</div>
 
 
</body>
</html>

Open in new window

0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22691655
You are forgetting that CSS is cascading, so the LAST style to be applied is the working one.  Hence you don't change the class at all, you change the inline style -- but each DIV will need a unique ID, and you have listed NO IDs above, so substitute the right ID in this --

document.getElementById('DivId').style="whatever you want";


0
 

Author Comment

by:thyros
ID: 22693652
@Scrathcyboy

As I mentioned in the original post, the html is being repeated by a dynamic templating system which I have limited control over.. so it is not possible for me to add a unique ID to each div.

@Kravimir

This seems to work.  Thanks.  I want to test it a little further just to make sure there is no unexpected issues before closing the question.  Also, could you please advise what changes need to be made if I wanted to add the following logic:

If .promotext element contains more than 150 characters (including spaces) then apply css property {cursor:pointer;} to the element.

Thanks again.


0
 
LVL 42

Expert Comment

by:David S.
ID: 22693851
You're welcome.

The problem with doing that is if the font-size is changed, even though the max-height is set in EMs, unless the width is also set in EMs, the number of lines of text might change, thus making the 150 characters an inexact number, so the cursor wouldn't always be changed. I'd recommend that you just add the cursor declaration to the existing style rule.

I should probably mention that the code I posted is an example and that it can be improved. One way would be by using an event handing system instead of the limited DOM0 event handlers.

Have you considered what happens when someone who does not have JavaScript enabled in his/her browser visits your page?
0
 

Author Closing Comment

by:thyros
ID: 31505199
Thanks.
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
function parameter and using that as array 15 23
jQuery force form POST 7 45
Html input text color the frame line 2 20
Search Item in Table 2 19
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to count occurrences of each item in an array.
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…

840 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