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!

DOM/Global Var Problem

Status
Not open for further replies.

Andrew1985

Programmer
Feb 27, 2006
3
CA
Hi there, I'm trying to code myself a dynamic editing system for my forums that lets a user double click their message and edit it right there rather than going through a 2 page step. I've run into some snags in the frontend... Here's what I have so far


function init()
{
var textareas = document.getElementsByTagName('textarea');
for (i = 0; i < textareas.length; i++)
{
var thisId = textareas.id.substr(1);
referenceDiv = document.getElementById('b' + thisId);
referenceDiv.onmouseover = function () { this.className = 'edithover'; }
referenceDiv.onmouseout = function () { this.className = 'cell2'; }
referenceDiv.ondblclick = function () {
editArea = document.getElementById('e' + thisId);
editArea.value = this.innerHTML;
editArea.style.display = '';
editArea.focus();
this.style.display = 'none';
}
}
}

window.onload = init;
</script>
</head>
<body>
<div class=cell1 id=m55637><STRONG>From:</STRONG> Andrew | Owner | <STRONG>Posted:</STRONG> 2/21/2006 4:53:17 PM | #0007 </div>
<div id="b55637" class=cell2>I loved that game.
<br />
---
<br />This is a signature</div>
<div style="padding:0;margin:0"><form action="#"><textarea id="e55637" class="editbox" name="messageBody" style="display:none"></textarea></form></div>

<div class=cell1 id=m55638><STRONG>From:</STRONG> Andrew | Owner | <STRONG>Posted:</STRONG> 2/21/2006 4:53:17 PM | #0008 </div>
<div id="b55638" class=cell2>I enjoyed this game a lot
<br />
---
<br />This is a signature</div>
<div style="padding:0;margin:0"><form action="#"><textarea id="e55638" class="editbox" name="messageBody" style="display:none"></textarea></form></div>

<div class=cell1 id=m55639><STRONG>From:</STRONG> Andrew | Owner | <STRONG>Posted:</STRONG> 2/21/2006 4:53:17 PM | #0009 </div>
<div class=cell2 id="b55639">I did not enjoy this game
<br />
---
<br />This is a signature</div>
<div style="padding:0;margin:0"><form action="#"><textarea id="e55639" class="editbox" name="messageBody" style="display:none"></textarea></form></div>

What's wrong with this is that whenever I dblclick a div the information/events are all triggered to the last textarea rather than their respected textareas. Could anyone out there help out and explain why this is happening and how to fix it?

Thanks!
 
You can do the thidId exercise within the dblclick handler to decouple the scoping problem.
[tt]
referenceDiv.ondblclick = function () {
editArea = document.getElementById('e' + [red]this.id.substr(1)[/red]);
//...etc etc
}
[/tt]
 
It is different div's but related to one another.
 
Ah well. I guess it's just that I would have done things differently (moving the substr call to reference the elements beginning with "b" only, and using this.id to reference the textareas).

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top