JQuery animation div width onclick

I have no idea why this is not working.

JQuery

$("#foldit").click(function () {
    $("foldit").animate({"width": "165px"}, "fast");
});
+4
source share
3 answers

This is because you missed #in your selector.

Just try using the link thisinside this click event to achieve what you want,

$("#foldit").click(function () {
    $(this).animate({"width": "165px"}, "fast");
});

According to your new requirement, you can try this,

$('#foldit').click( function() {
    var toggleWidth = $(this).width() == 165 ? "100px" : "165px";
    $(this).animate({ width: toggleWidth });
});

Demo

+5
source

You missed '#'in the selector and you can also use it in the same way

$("#foldit").click(function () {
    $(this).animate({"width": "165px"}, "fast");
});
+2
source

Use $(this)instead$("foldit")

+2
source

Source: https://habr.com/ru/post/1524817/


All Articles