Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Set div height to the img inside it’s height

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #29760
    noahgelman
    Participant

    I have a click function that copys a related image and prepends that image into a div. But then I also want to set that divs height and width to be the same height as the image. Here is kind of what I have so far.

    Code:
    $(function() {
    var $example = $(‘div#example’);

    $(‘a.link’).click(function() {
    $(this).siblings().clone().prependTo(‘#example’);

    var imgWidth = $(‘#example img’).css(‘width’);
    imgHeight = $(‘#example img’).css(‘height’);

    $example.css(‘width’, ‘imgWidth’);
    $example.css(‘height’, ‘imgHeight’);

    });
    });

    As you can see, it’s not too complicated. I believe the syntax for the css at the bottom is wrong but I tried a couple things and it didn’t seem to work. Any ideas?

    #80484
    noahgelman
    Participant

    Got it :D although I feel bad that I just wasted some forum space but maybe this’ll help someone later. I changed css to just .width() and .height() and tossed in the variables. Easy peasy.

    Code:
    $(function() {
    var $example = $(‘div#example’);

    $(‘a.link’).click(function() {
    $(this).siblings().clone().prependTo(‘#example’);

    var imgWidth = $(‘#example img’).css(‘width’);
    imgHeight = $(‘#example img’).css(‘height’);

    $example.width(imgWidth);
    $example.height(imgHeight);

    });
    });

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.