1

I want a button to handle a certain JQuery function, but it is not doing anything.

The script inside my .cshtml file:

<script src="~/lib/jquery/dist/jquery.min.js">
    jQuery(function ($) {
        $('#swapHeart').on('click', function () {
            var $el = $(this),
                textNode = this.lastChild;
            $el.find('span').toggleClass('glyphicon-heart glyphicon-heart-empty');
            $el.toggleClass('swap');
        });
    });
</script>

and the button in my .cshtml file:

<button id="swapHeart" class="btn btn-default swap">
    <span class="glyphicon glyphicon-heart-empty"></span>
</button>

Why is this not working? Thanks in advance. EDIT: It does work in this codepen I created: https://codepen.io/floorvrmt/pen/qQPEKQ

1 Answer 1

2

Your code using the same script tag which is used to load the source of jQuery. Check the code below, in which I load the source and script

<script src="~/lib/jquery/dist/jquery.min.js">
</script>
<script>
    jQuery(function ($) {
        $('#swapHeart').on('click', function () {
            var $el = $(this),
                textNode = this.lastChild;
            $el.find('span').toggleClass('glyphicon-heart glyphicon-heart-empty');
            $el.toggleClass('swap');
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.