0

I've a PHP file hyper-linked and I'm calling a javascript function on click of link to check some condition and display alert box.

I simply want button to stop any action (Should not call PHP file) after I click the OK button in alert box if the condition two is true

My HTML code is

<a onclick="combocheck()" id="btnlabel" title="Buy Now" class="btn icon-Add To Cart" href="http://www.fxpure.in/cart.php?action=add&amp;product_id=86">Select Design</a>

After clicking the link, it add the item to the cart and opens a lightbox with details.

I have added a function

function combocheck()
        {
        var noitems = document.getElementById("cartitems").innerHTML;  
        if (noitems == "") {
            alert ("Your Cart is Empty, Please Proceed.");
        } else if (noitems != "") {
            alert ("Your cart already have some items, you can not purchase other items along with the combo");
        }
}

What I want is, if the condition 2 is true, the item should not be added to the cart and no lightbox should be displayed.

Clément Malet
  • 5,062
  • 3
  • 29
  • 48
Rajesh Kumar
  • 17
  • 1
  • 6
  • 1
    This sort of questions has [been asked](http://stackoverflow.com/questions/7056669) and [also answered](http://stackoverflow.com/questions/7056669/prevent-default-event-in-onclick/7056673#7056673). – Linus Kleen Aug 14 '14 at 10:35

1 Answers1

0

just change onlick event to onclick="return combocheck();" to return value to event handler

and after alert write: return false; to tell event handler that don't do anything.

Adarsh Rajput
  • 1,246
  • 14
  • 24
  • Hi, I tried this but it is not working, I think some other jquery function is also running on the link click. Is there any way I can prevent another function. – Rajesh Kumar Aug 18 '14 at 11:13
  • @rk1708 May be jQuery is changing your onclick function... to prevent this you need to check all JavaScript code(is the only option). I know that is typical so I have one trick: **Remove/Change the `id='btnlabel'` attribute, if not work then change anchor-A tag to `SPAN, DIV or P` tag-whatever you want, and also try with changing all `class=""` attribute.** Coz, jQuery will be applying onclick event through `Id` or `tagname` or `class name` only. **If it does not work then you have to check whole JavaScript code.** Coz at my end adobe code is working, I checked. – Adarsh Rajput Aug 18 '14 at 11:45