Javascript and height of an element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The Devil
    New Member
    • Dec 2007
    • 8

    Javascript and height of an element

    Hi,

    I'm facing a problem with this one. I made a js script which checks the height of an element and increases it with 30px. Everything is working fine but then comes the problem with summing the height and 30 px.

    This is my code:
    Code:
    function add(elmnt)
    {
        if(elmnt != null || elmnt != 'undefined')
        {
             var height = document.getElementById(elmnt).style.height;
             var check = /px/gi;
             var clearHeightPx = height.replace(check, '');
             var clearHeight = clearHeightPx.replace(/^\s+|\s+$/g, "");
             var newHeight = eval(clearHeight+30);
             alert(newHeight);
        }
    }
    html:
    Code:
    <div id="div1" style="height: 200px;">
    <img src="justanimage" alt="justanimage" onclick="add('div1')">
    </div>
    The alert is to show the result. Die to the fact it's not summing I didn't made the code which changes the height of the element but that's a easy one ;)


    Any help will be appreciated !

    Thanks in advance,
    Steve
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    welcome to TSDN ...

    please forget about eval ... eval == evil :) ... don't use it except in case you need to evaluate a json-string ...

    the following works for you:

    [CODE=javascript]var newHeight = parseInt(clearH eight, 10) + 30;[/CODE]
    kind regards

    Comment

    • The Devil
      New Member
      • Dec 2007
      • 8

      #3
      Originally posted by gits
      hi ...

      welcome to TSDN ...

      please forget about eval ... eval == evil :) ... don't use it except in case you need to evaluate a json-string ...

      the following works for you:

      [CODE=javascript]var newHeight = parseInt(clearH eight, 10) + 30;[/CODE]
      kind regards
      Thanks. That works fine and it's exactly what I needed :)
      Best wishes

      Comment

      Working...