Div top value

Discussion in 'JavaScript' started by Tachyon Informatics, May 23, 2011.

  1. #1
    hi friends. i need a help

    i want to disply a div below of a text box.

    i use this script to do it :
    but div displaying extractly top on text box , so change it like below

    but this time it's nt working . what is the mistake ?

    ** i am using ' position:absolute; ' for div
     
    Tachyon Informatics, May 23, 2011 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Try:
    
    document.getElementById('fi').style.top = String(parseInt(document.getElementById('textfield').style.top) + 14) + 'px'; 
    
    Code (markup):
     
    rainborick, May 23, 2011 IP
  3. Tachyon Informatics

    Tachyon Informatics Member

    Messages:
    113
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    40
    #3
    not working my dear rainborick :(
     
    Tachyon Informatics, May 23, 2011 IP
  4. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #4
    The code I suggested would work if the script had previously set the 'top' setting on the element 'textfield' or if the tag for 'textfield' has a 'style' attribute that includes the 'top' setting. If you haven't set a specific value of 'top' for 'textfield', you can try calling getComputedStyle and parsing the information, but it's easier if you just set the value in the first place so JavaScript can see and work with it. Either add a 'style' attribute to the tag or set the value using JavaScript. Good luck!
     
    rainborick, May 23, 2011 IP
  5. Sefrez

    Sefrez Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You cannot add the top style attribute to the div element without first changing it's position attribute to either absolute or relative:
    
    document.getElementById('fi').style.position = 'absolute';
    document.getElementById('fi').style.top = parseInt(document.getElementById('textfield').style.top) + 14 + 'px';
    
    Code (markup):
    As you see, I also added parseInt() and (+ 'px'), that is because, if style.top is of string data type in pixels, you must have only the integer to add it to 14. I assume the 14 is the height of the textfield, I believe you could also do:
    
    document.getElementById('fi').style.position = 'absolute';
    document.getElementById('fi').style.top = parseInt(document.getElementById('textfield').style.top) + document.getElementById('textfield').clientHeight + 'px';
    
    Code (markup):
     
    Sefrez, May 23, 2011 IP