Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery animate div's width onclick [closed]

I have no idea why this doesn't work.

JQuery:

$("#foldit").click(function () {
    $("foldit").animate({"width": "165px"}, "fast");
});
like image 356
Shaul Bar-Lev Avatar asked Jan 12 '23 07:01

Shaul Bar-Lev


2 Answers

Its because you have missed # in your selector.

Just try to use the this reference inside that click event to achieve what you want,

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

As per your new requirement you can try like this,

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

DEMO

like image 93
Rajaprabhu Aravindasamy Avatar answered Jan 21 '23 13:01

Rajaprabhu Aravindasamy


You missed '#' in the selector and alternatively you can use it like this as well

$("#foldit").click(function () {
    $(this).animate({"width": "165px"}, "fast");
});
like image 31
Sachin Avatar answered Jan 21 '23 12:01

Sachin