function scrollDetect(elemId) {
var elem = document.getElementById(elemId);
while (elem.scrollHeight > elem.clientHeight) {
alert('test!')
}
}
scrollDetect('pmessage');
While loop does not work
Page 1 of 19 Replies - 515 Views - Last Post: 26 June 2011 - 02:21 PM
#1
While loop does not work
Posted 23 June 2011 - 11:53 PM
Replies To: While loop does not work
#2
Re: While loop does not work
Posted 24 June 2011 - 11:14 AM
#3
Re: While loop does not work
Posted 24 June 2011 - 01:49 PM
#4
Re: While loop does not work
Posted 24 June 2011 - 02:25 PM
If you take it out, it takes less page weight, anyway.
#5
Re: While loop does not work
Posted 24 June 2011 - 06:01 PM
maniacalsounds, on 24 June 2011 - 02:25 PM, said:
If you take it out, it takes less page weight, anyway.
You're misunderstanding what I'm trying to do. I have it set to overflow:auto already. What I am trying to do is check if the scrollbar is there, then I will reduce the text until the scrollbar is no longer there and add a "..." at the end of it. I don't want their to be scrollbars I just want to check if the posted text is longer than the allotted space.
#6
Re: While loop does not work
Posted 25 June 2011 - 07:22 PM
function scrollDetect(elemId) {
var elem = document.getElementById(elemId);
if (elem.scrollHeight > elem.clientHeight) {
var newContent = elem.innerHTML;
newContent = newContent.slice(0, -5)
elem.innerHTML = newContent;
} else {
var i = true;
}
}
var i = false;
while (i === false)
{
scrollDetect('pmessage$id');
}
Any help is appreciated, thank you.
This post has been edited by macosxnerd101: 26 June 2011 - 02:19 PM
Reason for edit:: Fixed code tags
#7
Re: While loop does not work
Posted 26 June 2011 - 11:00 AM
P.S.
If you're dealing with an input element then you should be using the value property instead of innerHTML.
I just saw your other thread where you describe what you're trying to do.
I was playing around with the scrollHeight and clientHeight properties and I've come up with this... Explanation in the comments
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function scrolldetected(ele)
{
//debugging purposes
document.getElementById('results').innerHTML = "Scroll Height: " + ele.scrollHeight;
document.getElementById('results').innerHTML += "<br />Client Height: " + ele.clientHeight;
///////
if(ele.scrollHeight > ele.clientHeight) //this means that the user has entered more text than the element can hold
{
//note I used value and not innerHTML here
ele.value = ele.value.substr(0, ele.value.length - 1); //A newline character is counted in the length of the text as 1. So therefore if the user reaches the end and causes the element's scrollHeight to grow we'll remove the last character which will take it back to the previous line.
}
}
</script>
</head>
<body>
<textarea onscroll="return scrolldetected(this)"></textarea>
<div id="results"></div>
</body>
</html>
This post has been edited by codeprada: 26 June 2011 - 11:11 AM
#8
Re: While loop does not work
Posted 26 June 2011 - 12:01 PM
codeprada, on 26 June 2011 - 11:00 AM, said:
P.S.
If you're dealing with an input element then you should be using the value property instead of innerHTML.
I just saw your other thread where you describe what you're trying to do.
I was playing around with the scrollHeight and clientHeight properties and I've come up with this... Explanation in the comments
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function scrolldetected(ele)
{
//debugging purposes
document.getElementById('results').innerHTML = "Scroll Height: " + ele.scrollHeight;
document.getElementById('results').innerHTML += "<br />Client Height: " + ele.clientHeight;
///////
if(ele.scrollHeight > ele.clientHeight) //this means that the user has entered more text than the element can hold
{
//note I used value and not innerHTML here
ele.value = ele.value.substr(0, ele.value.length - 1); //A newline character is counted in the length of the text as 1. So therefore if the user reaches the end and causes the element's scrollHeight to grow we'll remove the last character which will take it back to the previous line.
}
}
</script>
</head>
<body>
<textarea onscroll="return scrolldetected(this)"></textarea>
<div id="results"></div>
</body>
</html>
I appreciate the help, however I do not want to prevent users from entering more text than can be displayed. I simply want to reduce the text afterward to show excerpts (like emails or blogs) so then when a user clicks the post it will display the full post.
#9
Re: While loop does not work
Posted 26 June 2011 - 12:19 PM
#10
Re: While loop does not work
Posted 26 June 2011 - 02:21 PM
|
|

New Topic/Question
Reply



MultiQuote








|