Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script hangs browser

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
They say a little knowledge is a dangerous thing.

I'm trying to write a script that will allow users to toggle between word-wrap and no word-wrap. All my table cells are specified like this:

<TD><DIV class='wrap'>Here is text</DIV></TD>

By default my DIV.wrap style is { white-space: nowrap; }

I've tried to adapt an existing script so that it'll change the white-space from nowrap to normal but all it does is hang the browser. Any fixes would be gratefully received, and I'm happy to learn from this so if there's a better way of doing the same thing please let me know.

Code:
function setwhitespacetonormal() {
  var elements;
  elements = document.getElementsByTagName('div');
  for(var i = 0; i < elements.length; i++) {
    var node = elements.item(i);
    for(var j = 0; j < node.attributes.length; j++) {
      if(node.attributes.item(j).nodeName == 'class') {
        if(node.attributes.item(j).nodeValue == 'wrap') {
          node.style.whitespace = 'normal';
        }
      }
    }
  }
}

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Looping round attributes seems a bit bit convoluted to me, when you could jump directly to the class:

Code:
function setwhitespacetonormal() {
	var elements = document.getElementsByTagName('div');
	for(var i=0; i<elements.length; i++) {
		var node = elements[i];
		if (node.className == 'wrap') node.className = 'normal';
	}
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Ok - so this:

Code:
if (node.className == 'wrap') node.className = 'normal';

should have read:

Code:
if (node.className == 'wrap') node.style.whiteSpace = 'normal';

Although presumably you'd be better off changing the class as otherwise you would always fall into the 'if' regardless of whether the style had changed or not...

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top