Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Create label tag with attribute "for" with javascript.

Posted on 2006-05-13
5
Medium Priority
?
5,681 Views
Last Modified: 2013-11-19
Hi!

I'm trying to use Javascript to create a label tag like this:
---------------------------------------------------------
<label for='section'>Section name</label>
---------------------------------------------------------

and I use the Javascript:
===================================
var label = document.createElement('label');
label.for = 'section';
var text = document.createTextNode('Section name');
lable.appendChild(text);
===================================


But when I run this script It get some error at "label.for = 'section';";

So can you correct it for me please?
Thanks
0
Comment
Question by:tunhien
  • 3
5 Comments
 
LVL 17

Expert Comment

by:Cem Türk
ID: 16674185
<html><head>
</head>

<body>
<script language="JavaScript">
<!--
var label = document.createElement('label');
label.For = 'section';
var text = document.createTextNode('Section name');
label.appendChild(text);
document.body.appendChild(label)
//-->
</script>
</body></html>
0
 
LVL 17

Accepted Solution

by:
Cem Türk earned 500 total points
ID: 16674202
here is a improved version, this function should be called when the body is loaded, btw the differences between your first script and this one are;

when accessing label's for property we have to use label.For not label.for because its CaSe-SeNSiTiVe
we have to append newly created label element to the document with document.body.appendChild(label)

<html><head>
<script language="JavaScript">
<!--
function clabel(fr,txt) {
var label = document.createElement('label');
label.For = fr;
var text = document.createTextNode(txt);
label.appendChild(text);
document.body.appendChild(label); }
//-->
</script>
</head>

<body onload="clabel('section','Section name')">

</body></html>
0
 

Author Comment

by:tunhien
ID: 16674204
It's so simple!
:-D

Thanks so much
0
 
LVL 17

Expert Comment

by:Cem Türk
ID: 16674208
your welcome ;)
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 25110214
For others accessing this solution (as I did): the solution stating label.For is actually incorrect.  It should be label.htmlFor (which I found on other sites...)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question