manraj82
manraj82

Reputation: 6325

how to set width of an element in jQuery

How do I set the width of the #book to width of #clickme a ? If I say $(this).width() in the callback function it gives me the width of #book.There are more than one anchor tags in #clickme.I need to increase the width at the end of the animation,please have a look at let me know

$('#clickme a').click(function() {
  $('#book').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
  }, 5000, function() {
    //set width of #book to width of #clickme a item
  });
});

Thank You

Upvotes: 0

Views: 225

Answers (5)

Treemonkey
Treemonkey

Reputation: 2163

$('#book').width($("#clickme a").width())

Upvotes: 1

Senad Meškin
Senad Meškin

Reputation: 13756

$('#clickme a').click(function() {
  var _w = $(this).width();
  $('#book').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'

  }, 5000, function() {
     $(this).width(_w);
  });
});

Upvotes: 1

Erick Petrucelli
Erick Petrucelli

Reputation: 14932

Updated based on OP comment:

$("#clickme a").click(function() {
    var clicked = $(this);
    $("#book").animate({
        opacity: 0.25,
        left: "+=50",
        height: "toggle"
    }, 5000, function() {
        $(this).css("width", clicked.width())
    });
});

Upvotes: 4

Praveen Lobo
Praveen Lobo

Reputation: 7187

$('#clickme a').click(function() { 
    var aWidth = $(this).css("width");//alert(aWidth);
    $('#book').animate({
        opacity: 0.25,     left: '+=50',     height: 'toggle'   }, 500, 
        function() {   
            //set width of #book to width of #clickme a item   
            //alert($(this).css("width"));
            $(this).css("width",aWidth);
        });
}); 

Upvotes: 1

Anish
Anish

Reputation: 2927

You can try like this...

$('#clickme a').click(function() {
  $('#book').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
  }, 5000, function() {
     $(this).css('width','500');
  });
});

Upvotes: 0

Related Questions