File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes how to assign my function to onclick 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 "how to assign my function to onclick" Watch "how to assign my function to onclick" New topic
Author

how to assign my function to onclick

nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
Hi

I have an html page. I want to add a function to onclick event of the links in this page. I had used the following code but it doesn't work:

<a href="#" onClick="javascript:popup(200,200);return false">hello</a>

can you please let me know how I should write it?

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61312
    
  66

Omit the "javascript:" prefix.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
I have tried that. but when click on the link the function doesn't run
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

I don't see anything wrong. Have you defined popup method ? Are there any errors in the popup method?


- Chinna
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
yes, popup works correctly. popup is another function in the same java script file that the above code is.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

do you see any errors in the javascript consoles of browsers.. when you click the link ?
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
no, it just do nothing when I click on links.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Which browser ?
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
IE 7. I tested it with alert('hello') and it works with that but doesn't work with my function.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

I tried it in ie8, firefox 3.6, chrome.. working fine.

I can suggest these...

You need to check, how the code to rendered to browser. You can check the html source on the browser
Make sure you don't have any javascript errors in the page. If there are any, IE shows on the bottom status bar.
If you have other browsers check there, if it works. If you use firefox, you can check the javascript errors on javascript console.
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
ok, thanks. I have another question. suppose that I have a link like this: < a href=.....> hello</a> can you please tell me how I should get the name of the link, I mean "hello" in this example.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

<a href="#" id="mylinkId" onClick="javascript:popup(200,200);return false">hello</a>

Then you should use following statement in javascript to get the "hello"

document.getElementById("mylinkId").innerHTML;
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
many thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to assign my function to onclick