jeudi 27 juillet 2017

JavaScript else if statement

I've been trying to perform a simple jQuery code snippet for hours now. Embarrassingly, I haven't got it to work yet.

Here's the JS first and then the HTML:

jQuery(function ($) {
    if ($('#button1')) {
        $('#button1').on('click', function(event){
            console.log('First action');
        });

    } else if ($('button2')) {
        $('#button2').on('click', function(event){
            console.log('Second action');
        });
    }
});
<script src="http://ift.tt/1vtgOwa"></script>


<!-- Button 1 -->
<div id="button1">
    <a href="#">Action for button 1</a>
</div>

<!-- Button 2 -->
<div id="button1">
    <a href="#">Action for button 2</a>
</div>

Aucun commentaire:

Enregistrer un commentaire