File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Using javascript variables to execute html code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Using javascript variables to execute html code" Watch "Using javascript variables to execute html code" New topic

Using javascript variables to execute html code

Mkhaya Tini

Joined: Oct 06, 2005
Posts: 23
Hi all, here is my problem. I have a drop down list with two options, A and B. then a button named say, "OK". When A is selected and OK pressed, I need to execute certain html code, and when B is selected some other code. like a if and else statement but the flag is a drop down list (option). Can I do that? How? Please help.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

I do not know what you mean by "execute HTML code". DO you mean change the HTML somewhere on the page?

[Asking smart questions] [About Bear] [Books by Bear]
Tokai Namnai

Joined: Nov 02, 2005
Posts: 5
Sorry, if I'm not clear. Let me try again.

Think it this way: I've a button on a page that will refresh the page, but not exactly. When the first time the page loads from a link, it should execute the code that is in "if". When I click on the button, it should execute the code in "else". in other words, the look of the page will be different if its loaded from a url or by clicking on the button. please let me know if that makes sense.
Tokai Namnai

Joined: Nov 02, 2005
Posts: 5
....Or, please answer this question. In a .jsp file where there are java code, and html and javascript codes, can I assign a value to a JAVA variable using JAVASCRIPT code? if yes how?

i know I can say, var x = '<=%java_var%>' to read a variable. but how do i write?
[ November 09, 2005: Message edited by: Tokai Namnai ]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

No. Your JSP code (which may include Java scriptlets) is executed on the server. It generates HTML (which may include Javascript), which is then sent to the client -- your browser. The browser displays this HTML, and executes Javascript as necessary.

When you look at it this way, it should be pretty clear that executing Javascript in the browser can not have any effect on the JSP that has already finished generating HTML.

Now, you can't "execute" HTML code because HTML isn't executable. But it is possible to modify the appearance of the existing HTML via Javascript. There is a model of the HTML in the browser called a "DOM" which you can manipulate via Javascript. So you could make visible fields hidden, add new HTML, all sorts of things.
I agree. Here's the link:
subject: Using javascript variables to execute html code
It's not a secret anymore!