Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to assign my function to onclick

 
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
 
Marshal
Posts: 67445
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
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
 
    Bookmark Topic Watch Topic
  • New Topic