problem with javascript values

function Codes()

{

codeid=document.topics2.sublevels.value;

alert(codeid);//this alerts the value e.g 3

code ="<%=codeHash.get("%>codeid<%")%>";

alert(code);

document.topics2.code.value=code;

}

This gives null

Can anyone see the problem with the above. When I put in a value

code ="<%=codeHash.get("3")%>"; it works.

Thanks in advance

Ys

[443 byte] By [Smartie_ys] at [2007-9-26 1:31:59]
# 1
Is it code ="<%=codeHash.get("%>codeid<%")%>";ORcode ="<%=codeHash.get("<%=codeid%>")%>";?zakir
zakir_sh at 2007-6-29 1:32:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

codeid is the javascript value it e.g. "3"

code ="<%=codeHash.get("%>javascript value<%")%>";

it then gets the corresponding value from a hash table.

when I put the value in

code ="<%=codeHash.get("3")%>";

it works

and when I print out the codeid it gives me the right value 3. It is really strange

Smartie_ys at 2007-6-29 1:32:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
PLEASE SOMEBODY HELP!!!!!!!!!!!!!!!!!!!!
Smartie_ys at 2007-6-29 1:32:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> codeid is the javascript value it e.g. "3"

>

> code ="<%=codeHash.get("%>javascript value<%")%>";

Can't do that, I'm afraid. If you think about this a little, all the Java commands (in <% .. %> tags) get executed on the server, and the JSP generates what is basically a text file (an HTML page) that is sent to the browser. When this hppens, there are no <%..%> tags in your page any more - they are dead and gone forever.

OK, now the html page arrives at the browser, which parses it and runs the JavaScript code - so it gets your form field value, and then what does it do? It can't look it up in a hashtable, 'cos there *is* no hashtable - that's back on the server...!

So, to do this type of thing, you need to take these steps:

1) use javascript to get your value ("3" or whatever) and do a form POST or GET to the server to pass this value to your next JSP (or even back to the same JSP) on the server

2) the JSP then uses response.getParameter(...) to grab that value, and it can then do something with it.

artntek at 2007-6-29 1:32:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I get your point here but what I don't understand is in my select menu

<select onChange="update()">

<option></option>

</select>

then

function update()

{

code ="<%=codeHash.get3")%>";

alert(code);

}

This works. Why does this work. I need to be able to get the details without refreshing the page etc.

Smartie_ys at 2007-6-29 1:32:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> I get your point here but what I don't understand is

> in my select menu

> <select onChange="update()">

> <option></option>

> </select>

> then

>

> function update()

> {

> code ="<%=codeHash.get3")%>";

> alert(code);

> }

> This works. Why does this work.

This works because, as I said, the <% ... %> tags get interpreted as the page is first built (on the server), so at that time, the JSP engine is told to do this:

evaluate this expression: codeHash.get("3")

Which it does, thus replacing your method call "<%=codeHash.get("3")%>"with the actual value that's in your HashTable, associated with key "3".

Then, when it's finished writing the HTML page, it sends it to the client, with no <% %> tags in it.

On the other hand, your original line was this:

code =" <%=codeHash.get("%>codeid<%")%>";

If we break this down, we can see 2 sets of JSP tags:

<%=codeHash.get("%>...and<%")%>

The first of these says to the JSP engine:

evaluate this expression: codeHash.get("

.... to which the JSP engine would say - "No way, dude: that's not even a complete expression! :-)

>I need to be able to

> get the details without refreshing the page etc.

The only feasible workaround I can think of is to create a huge javascript array containing of the values in your hashtable. This would be generated on the server, and would become part of the <script> block in the page code. Your javascript function could then use the values in this array instead of the call to the hashtable.

artntek at 2007-6-29 1:32:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...