Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<a> onclick function not working

My function is not fired when the tag is clicked. Here is my code:

HTML:

<div data-role="footer">
    <div data-role="tabstrip" id="tabs"> 
        <a href="views/contacts.html" data-icon="home" id="home">Home</a>  
        <a href="views/settings.html" data-icon="settings" id="settings">Settings</a>
        <a onclick="signOff()" href="views/home.html" data-icon="settings" id="contacts">Log Out</a>
    </div>
</div>

JavaScript:

function signOff() {
    console.log("something");
    VCare.VCareWebService.signOff({cache:false,
        callback:function(xml) { // invoke the service
            // use jQuery to extract information we are interested in
            console.log(xml);
        }
   });
}
like image 568
btf217 Avatar asked Apr 08 '26 03:04

btf217


2 Answers

You can't have both an onClick function and valid href attribute in <a>. Change your anchor element to:

<a onclick="signOff()" href="javascript:void(0)" data-icon="settings" id="contacts">Log Out</a>

You can redirect the page using javascript if you want to.

like image 125
Basit Avatar answered Apr 09 '26 17:04

Basit


Another way is to make sure that your onclick returns a false to stop the default event from running.

<a onclick="signOff(); return false" href="views/home.html" data-icon="settings" id="contacts">Log Out</a>

Or..

<a onclick="return signOff();" href="views/home.html" data-icon="settings" id="contacts">Log Out</a>

function signOff() {
    console.log("something");
    VCare.VCareWebService.signOff({cache:false,
        callback:function(xml) { // invoke the service
            // use jQuery to extract information we are interested in
            console.log(xml);
        }
   });

   return false;
}
like image 21
Jeremy J Starcher Avatar answered Apr 09 '26 17:04

Jeremy J Starcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!