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 - 506 Views - Last Post: 26 June 2011 - 02:21 PM
#1
While loop does not work
Posted 23 June 2011 - 11:53 PM
I want to perform a loop while the scroll height of a div is larger than the set height (this will be done for excerpts) however the loop doesn't work (I realize this would be an endless loop). Yet it works when I replace "while" with "if".
Replies To: While loop does not work
#2
Re: While loop does not work
Posted 24 June 2011 - 11:14 AM
In what context do you want to use this? Whats the purpose of it?
#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
I understand what you're trying to do with this, but CSS would be a more viable option. It easily can create the automatic scrollbars, via overflow:auto;. Why are you wanting to manipulate this via Javascript?
If you take it out, it takes less page weight, anyway.
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:
I understand what you're trying to do with this, but CSS would be a more viable option. It easily can create the automatic scrollbars, via overflow:auto;. Why are you wanting to manipulate this via Javascript?
If you take it out, it takes less page weight, anyway.
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
So I want to be able to reduce text while a value is false until it becomes true, but it does not seem to be working.
Any help is appreciated, thank you.
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
Are you trying to limit the user to the amount of text that can be entered?
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
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:
Are you trying to limit the user to the amount of text that can be entered?
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
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
Well that's a different story. If you're not going to load the links dynamically (Ajax) then they'll be more than likely made via PHP. Cut the excerpts down with substr and you can add three dots or what not after to show that there's more to the user.
#10
Re: While loop does not work
Posted 26 June 2011 - 02:21 PM
Duplicate threads merged. Please avoid duplicate posting.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|