Two Laptop Bag*
The moose likes HTML, CSS and JavaScript and the fly likes Problrm in calling  userdefined method in onclick event of button dynamically 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 "Problrm in calling  userdefined method in onclick event of button dynamically" Watch "Problrm in calling  userdefined method in onclick event of button dynamically" New topic
Author

Problrm in calling userdefined method in onclick event of button dynamically

Rakesh Kumar
Ranch Hand

Joined: May 30, 2006
Posts: 63
Hi dear all,

I am dynamically creating html page as
document.writeln("<input type=\"button\" id=\"btn\" value=\"Table\" onclick=\"alert('xxxxxxxxxx'); test();\" />")

Here alert is calling ,but teste() method is not calling.I observerd that if we call any library function like print(), alert() it will be called.Pronlem for only userdefined functions.


Sanmple code is below
-----------------------
<HTML>
<HEAD>
<TITLE> Applet Test </TITLE>
<SCRIPT LANGUAGE="JavaScript">

function graphdisplay()
{
alert("graphdisplay");


document.writeln("<input type=\"button\" id=\"btn\" value=\"Table\" onclick=\"alert('xxxxxxxxxx'); teste();\" />")

}

function test()
{
alert("teste---------------success")
}

</SCRIPT>
</HEAD>

<body onload="graphdisplay();">
<form id="inqform" name="inqform">
<script> </script>

</form>
</body>
</HTML>


Please respond ASAP if anybody have any solution very urgrnt.

thanks
rakesh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

This is the Java in general forum -- ie. a forum for Java questions. It would help if you asked the question in the correct forum.

Anyway, I'll move it to the Javascript forum for you...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Well to start with this is Javascript and not java where you posted it.
So you are getting the first alert (xxxxxxxxxx)?


What is wrong with just putting the button on in html rather than constructing it in javascript?

Sean


I love this place!
Rakesh Kumar
Ranch Hand

Joined: May 30, 2006
Posts: 63
No ihave to dynamically switch between graph and table with one html.So i hav eto use document.write to create page.Please reply if you have any idea
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
you CAN NOT use document.write AFTER the page loads.

Do a view source on the page. You will see that document.write REPLACES the ENTIRE document.

If you want to switch, you need to either have the content on the page already, you need to just show it with css [display: block/none] or you need to use DOM methods [createElement/appendChild/removeChild] OR you use innerHTML.

Eric
 
 
subject: Problrm in calling userdefined method in onclick event of button dynamically
 
Similar Threads
Scope Chain / Closures
Form Validation Help Needed Please
How do I conditionally remove link from jsp?**********
Problem with javascript include
Changing the text of a buton