-2

I'm trying block the event of a link, to don't add a product in a addToCart Button but is not working fine, the link is enabled yet, have idea what's wrong? Thanksss!!!

$(document).ready(function(){ 
    function buyButtonVerify() { 
        $('.buy-button.buy-button-ref').on('click', function(ev){
            ev.preventDefault();
        });
    }
    $('[name=text1], [name=text2]').keyup(function(){
        if($(this).val().length !=0){
            $('.buy-button').removeClass('enable');
            $('.buy-button').click(buyButtonVerify);
        }
    });
    $('[name=text1], [name=text2]').keydown(function(){
        if($(this).val().length !=0){
            $('.buy-button').removeClass('enable');
            $('.buy-button').click(buyButtonVerify);
        }
    });
    $('[name=text1], [name=text2]').focus(function(){
        if($(this).val().length !=0){
            $('.buy-button').removeClass('enable');
            $('.buy-button').click(buyButtonVerify);
        }
        
    })
});
<input type="text" placeholder="text 1*" name="text1" class="text1" data-target="previewText1" maxlength="11" />
<input type="text" placeholder="text 2*" name="text2" class="text2" data-target="previewText2" maxlength="11" />
<br />
<br />
<a target="_top" class="buy-button buy-button-ref" href="/redirect=true&amp;sc=1" style="display:block">Comprar</a>
Anna A.
  • 65
  • 1
  • 9
  • Need the HTML to see if the selectors are correct. – zer00ne Jun 20 '17 at 21:31
  • I updated with the HTML – Anna A. Jun 20 '17 at 21:39
  • So the objective is to have the button disabled until something is typed in either textbox? – zer00ne Jun 20 '17 at 21:57
  • Yes, i need deactivate the event click of this button when the inputs are on keyup, on keydown and on focus. – Anna A. Jun 20 '17 at 22:05
  • OK so the button works but it's disabled as soon as user types in the textboxes? That seems backwards. keyup and keydown are only momentary, while focus is perpetual until the user clicks elsewhere (like for instance the button) – zer00ne Jun 20 '17 at 22:08
  • The problem is, i have these 2 fields (text1 and text2), and i'm trying to block this button when the fields are focused or on key up and on key down, cause the platform has a time of delay to verify if the words exists in the table (mysql), and this fields cannot be sended while this verification isn't validate, so when i fill out the last field and after that click on the buy-button this is doing the addToCart event, i need block these event while the field is focused, but this isn't working at all. I was able to disable the button, but the link is still working even though it is disabled. – Anna A. Jun 21 '17 at 16:57
  • Ah, OK that sounds logical, review my updated answer, I think I almost got it. This part is confusing: *"after that click on the buy-button this is doing the addToCart event, i need block these event while the field is focused"* When you click the buy-button it becomes focused not the field. – zer00ne Jun 21 '17 at 19:23

1 Answers1

2

You are capturing the wrong click event. Use this instead.

  $('.buy-button.buy-button-ref').on('click', function(e){
        e.preventDefault();
    });

You need to capture the event from the click, not from the parent element.

Korgrue
  • 3,430
  • 1
  • 13
  • 20