how to assign a id as a variable

I

ilovesss2004

Hi,
Here is my code:

<html>
<head>
<script>
var a="hello"
</script>
</head>
<body>
<input type="button" value="click" id=a onclick="alert(id)" />
</body>
</html>

But when I click the button. It shows "a" instead of "hello". I want
to store some information to the id of a button by a function, and the
function must be in the head section, not body section. Can someone
tell me how to do this?

Thanks alot.
 
R

Richard Cornford

Hi,
Here is my code:

<html>
<head>
<script>
var a="hello"
</script>
</head>
<body>
<input type="button" value="click" id=a onclick="alert(id)" />
</body>
</html>

But when I click the button. It shows "a" instead of "hello".
I want to store some information to the id of a button by a
function, and the function must be in the head section, not
body section. Can someone tell me how to do this?

<html>
<head>
<script type="text/javascript">
var idToStringMap = {
"a":"hello from a",
"b":"hello from b"
};
function showMappedString(inputField){
alert(idToStringMap[inputField.id]);
}
</script>
</head>
<body>
<input type="button" value="click a"
id=a onclick="showMappedString(this);">
<br>
<input type="button" value="click b"
id=b onclick="showMappedString(this);">
</body>
</html>

Richard.
 
D

Denis McMahon

Hi,
Here is my code:

<html>
<head>
<script>
var a="hello"
</script>
</head>
<body>
<input type="button" value="click" id=a onclick="alert(id)" />
</body>
</html>

But when I click the button. It shows "a" instead of "hello". I want
to store some information to the id of a button by a function, and the
function must be in the head section, not body section. Can someone
tell me how to do this?

try: onclick="alert(eval(this.id))"
instead of: onclick="alert(id)"

There are other things you might want to sort out too:

1) /> closing style implies xml which needs a dtd
2) in which case the script tag requires a type attribute
3) and the id attribute might need to be quoted

The following is my full solution to your original problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Display value of variable pointed to by element id</title>
<script type="text/javascript">
var a="hello";
</script>
</head>
<body>
<p><input type="button" value="click" id="a" onclick=
"alert(eval(this.id))"></p>
</body>
</html>

Rgds

Denis McMahon
 
D

David Mark

try: onclick="alert(eval(this.id))"
instead of: onclick="alert(id)"
Don't.


There are other things you might want to sort out too:

1) /> closing style implies xml which needs a dtd

No, it indicates the prevailing trend of writing invalid HTML due to a
misunderstanding about XHTML (which has been a dead issue on the Web
for a decade).
2) in which case the script tag requires a type attribute

The script element should have a type attribute, but that has nothing
to do with XML.
3) and the id attribute might need to be quoted

It certainly should be quoted, but this has nothing to do with XML
either. The concept is valid HTML or not.
The following is my full solution to your original problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Display value of variable pointed to by element id</title>
<script type="text/javascript">
      var a="hello";
</script>
</head>
<body>
<p><input type="button" value="click" id="a" onclick=
"alert(eval(this.id))"></p>
</body>
</html>

You virtually never need to use eval (and certainly not for this).
See previous follow-ups for better solutions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top