Arun
Arun

Reputation: 655

Javascript form submission onclick not working in Google Chrome

I am trying to submit a form onclick of an anchor. Here is the code:

function submitLink() {
    document.forms["background-form"].submit();
    return false;
}

This code works in FF and IE but not in Chrome. Anyone know why? I am calling the function like so:

<a href="<?php echo "background.php?cred=".$cred['id']; ?>" onclick="submitLink();">Remove</a>

Upvotes: 1

Views: 3807

Answers (2)

Vignesh Pichamani
Vignesh Pichamani

Reputation: 8070

If you have found that onClick event does not work on Google Chrome, then here is quick way to fix it:

Replace onclick form id with its name, For example, let’s say you have the following:

<form id="form1" name="checkform1"

Find JS event:

onclick="document.form1.cSSld.value=''"

Replace with:

onclick="document.checkform1.cSSld.value=''"

All set. It now works with all web browsers.

Upvotes: 0

Willem D&#39;Haeseleer
Willem D&#39;Haeseleer

Reputation: 20180

You need to return false from the onclick function to prevent the default action. Otherwise you will just follow the link.

Try it like this

<a href="<?php echo "background.php?cred=".$cred['id']; ?>" onclick="return submitLink();">Remove</a>

I should also note that your link isn't an anchor link, an anchor link needs to have a fragmentation identifier. your link is just a link, that upon clicked, will cause the browser to navigate to it by means of a http get request, causing the current page to unload.

However it would be, much, better to bind the event handler trough jQuery, and use the built in preventDefault functionality.

Read more about it here

http://api.jquery.com/event.preventDefault/

Upvotes: 2

Related Questions