• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to assign my function to onclick

 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omit the "javascript:" prefix.
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried that. but when click on the link the function doesn't run
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything wrong. Have you defined popup method ? Are there any errors in the popup method?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, popup works correctly. popup is another function in the same java script file that the above code is.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you see any errors in the javascript consoles of browsers.. when you click the link ?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, it just do nothing when I click on links.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which browser ?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IE 7. I tested it with alert('hello') and it works with that but doesn't work with my function.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic