Some buttons to submit with default actions others to prevent default

Viewed 28

I understand how to stop default action with preventdefault(), but I have not found a way to let some buttons default to their original submit action while preventing other buttons. I would think that 'break' would work, but not so. Can someone assist me? Thanks,



$(".btn").click(function() {
    var clickedID = this.id 
    
    if (clickedID == 'admin_configs') {
        break     
    }
    
    else if (clickedID == 'btn_get_cfg') {
        msg='Your Configuration has been Retrieved'
        rslt = {
            btn_click: clickedID,
            data: $( "#btn_select option:selected" ).text()
        }
        populateForm(rslt,msg)
    
    }})
    
1 Answers

Assuming you want to prevent default where you have the break currently printed, you can just use preventDefault there. You just have to make sure to call it on the event itself, which is the function argument.

$(".btn").click(function(event) {
    var clickedID = this.id 
    
    if (clickedID == 'admin_configs') {
        event.preventDefault();     
    }
    
    else if (clickedID == 'btn_get_cfg') {
        msg='Your Configuration has been Retrieved'
        rslt = {
            btn_click: clickedID,
            data: $( "#btn_select option:selected" ).text()
        }
        populateForm(rslt,msg)
    
    }
})
Related