From: ilovesss2004 on
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.
From: Andrew Poulos on
On 26/07/2010 8:59 PM, ilovesss2004 wrote:

> <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?

What you want to do is very odd indeed, and I don't recommend doing it,
but you could try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Odd</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript">
window.onload = function() {
var tmp = document.getElementById("a");
if (tmp) {
document.getElementById("a").id = "hello";
}
};
</script>
</head>
<body>
<input type="button" value="click" id="a" onclick="window.alert(this.id)">
</body>
</html>

Andrew Poulos
From: Richard Cornford on
On Jul 26, 11:59 am, ilovesss2004 wrote:
> 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.
From: Denis McMahon on
On 26/07/10 11:59, ilovesss2004 wrote:
> 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
From: David Mark on
On Jul 26, 10:49 am, Denis McMahon <denis.m.f.mcma...(a)googlemail.com>
wrote:
> On 26/07/10 11:59, ilovesss2004 wrote:
>
>
>
>
>
> > 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)"

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.