...

View Full Version : Dynamic label doesn't respond



badmuts
12-07-2005, 02:15 PM
In the following code, I add a label to a div. I expect, when I click this new label (Some error), the textbox will get focus (like the 'Click here'-label does). But it doesn't. When you run this example, you'll notice there's nothing wrong with the on-the-fly created html, shown in the textarea. BTW: I use IE 6.0.



<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
function setError(obj, melding){
infoNode = document.getElementById("errors");

errorLabel = document.createElement("label");
var bullet_text = document.createTextNode(melding);
errorLabel.appendChild(bullet_text);
errorLabel.setAttribute("for", obj.id);
infoNode.appendChild(errorLabel);

// check created in textarea
document.all["putHere"].value = document.all["errors"].innerHTML;
}
//-->
</SCRIPT>

<body onload="setError(document.all['getFocus'], 'Some error');">
<div id="errors"></div>
<label for="getFocus">Click here</label><input type="text" id="getFocus">
<br><textarea id="putHere" rows=10 cols=80></textarea>
</body>
</html>

Kor
12-07-2005, 03:00 PM
a lot of IE only and weird things in your code...
For instance, one of them:


I expect, when I click this new label

Where is your onclick event?

Can you explain us what in fact you want to do?

badmuts
12-07-2005, 03:10 PM
You don't need an onClick-event, because of the for-attribute. This normally causes the textbox to get focus when the label is clicked (for-attribute has to contain the textbox's id). Try clicking on the label in front of the textbox.

Why do I want this. I building a web app, in which we want to show (multiple) client side errors in the div. We want these errors to be clickable, so the corresponding control gets focus. Yes. I know this can be done with <a href>'s, but labels is cleaner.

Made a small change to the sample code (innerHTML was outerHTML). This edited example runs on firefox. In that case the dynamic label does fire when clicked and the textbox gets focus. The problem is, our company doesn't use firefox, but IE.

Kor
12-07-2005, 03:16 PM
You don't need an onClick-event, because of the for-attribute. This normally causes the textbox to get focus when the label is clicked (for-attribute has to contain the textbox's id). Try clicking on the label in front of the textbox.
Hm ... never heard about this. To trigger a function you need an event. This is a basic javascript core... So, you need to focus a textarea when click on the label placed in front of it?

GJay
12-08-2005, 07:55 PM
No, the <label> tag should do that automatically.
<label for="email">Email address:</label><input id="email" name="email type="text" />
Will focus on the input when the label is clicked, forms should always be made like this.
Sorry, don't know why it's not working though, there's nothing obviously wrong...

badmuts
12-09-2005, 11:16 AM
Found the solution:

instead of

errorLabel.setAttribute("for", obj.id);
use

errorLabel.htmlFor = obj.id;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum