Solved

<a> tag's "onclick" event not working

Posted on 2009-07-07
18
355 Views
Last Modified: 2012-05-07
in the attached code snippet, onclick event is not working.

When i click on the "SUBMIT" link, i'm able to see the alert box with message "Came HERE". But, "frmNew" is not submitted.
Please let me know if i need to do anything in "fun()" without changing anything in the anchor tag <a>
<a id="errBoxCloseLink" href="javascript:void(0);" onclick="fun()" style="color:white;font-weight:bolder;text-decoration:none" title="Close Errors" linkcontrol="ignore">SUBMIT</a>
 
<script>
function fun()
{
    alert("Came HERE");
    document.forms["frmNew"].submit();
}
</script>

Open in new window

0
Comment
Question by:Ravi Kalla
  • 3
  • 3
  • 2
  • +3
18 Comments
 
LVL 7

Expert Comment

by:Valleriani
ID: 24793209
Are you defining the form name?

<form name="frmNew">
<a id="errBoxCloseLink" href="javascript:void(0);" onclick="fun()" style="color:black;font-weight:bolder;text-decoration:none" title="Close Errors" linkcontrol="ignore">SUBMIT</a>
</form>

<script>
function fun()
{
    alert("Came HERE");
    document.forms["frmNew"].submit();
}
</script>

?
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24793259
Hi,

remove the style attribute from the line and use:

<a id="errBoxCloseLink" href="javascript:void(0);" onclick="fun()" class="vClass" title="Close Errors" linkcontrol="ignore">SUBMIT</a>

and then

<style type="text/css">
a.vClass:link {color: blue;}
</style>

Hope this will help you.

Regards
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24793292
hi,

sorry..you should check if there you defined the form name as commented by  ' Valleriani: '

Regards
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Author Comment

by:Ravi Kalla
ID: 24793381
yes... i have already defined the form name as in the attached code
 

<script>
function fun()
{
    alert("Came HERE");
    document.forms["frmNew"].submit();
}
</script>
 
<a id="errBoxCloseLink" href="javascript:void(0);" onclick="fun()" style="color:black;font-weight:bolder;text-decoration:none" title="Close Errors" linkcontrol="ignore">SUBMIT</a>
 
<form name="frmNew">
</form>

Open in new window

0
 
LVL 7

Expert Comment

by:Valleriani
ID: 24793411
Tested in IE/FF, they seem to be both submitting though. (Page is reloading basicly)

Can we get some more details? Is it tied in another script? Browser version? etc?
0
 

Expert Comment

by:gshahlot
ID: 24793442
You might wanna try running these on some other browsers.
0
 
LVL 6

Author Comment

by:Ravi Kalla
ID: 24793520
i'm able to submit by changing the value of "href" to "javascript:fun()"
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 24793550
Hrm, could void(0) be interfering with some browsers? Could try:

<a id="errBoxCloseLink" href="#" onclick="fun()" style="color:white;font-weight:bolder;text-decoration:none" title="Close Errors" linkcontrol="ignore">SUBMIT</a>
0
 
LVL 13

Accepted Solution

by:
Murali Murugesan earned 500 total points
ID: 24793997
in "fun()" add return false; as the last statement.

-Murali*
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24829750
Ravi_Kalla,
I am going to post this as an objection.  The expert's post at http:#a24793997 has the key to your solution.  You need return false to follow the script or the browser just goes to the href as a natural next step.
The reason I post this as an objection is it isn't correct to have javascript in the href tag.  Depending on the doctype and browser you could even have problems.  Even the "javascript: void(0);" code isn't recommended in href anymore.  It is better to use # (as suggested in http:#a24793550) or even best to provide an html page that will show some "Javascript needs to be supported" message.  The key is the proper fix isn't to move the Javascript but to use return false so the event handling stops.
Let me know if you have a question about this.  It is worth changing your code because, even if the comment above was posted after you started closing this, it is the correct answer and way to do this.
b0lsc0tt
0
 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 24837024
Should there be a points split?

-Murali*
0
 
LVL 6

Author Comment

by:Ravi Kalla
ID: 24869383
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question