Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to change the text position in javascript

Tags:

javascript

I am making four buttons (input type="button") in html and want them to run function on click but nothing happening.

the html code is:

<div id="newtext" style="position:absolute; top:100px;">
    <p>this is Text</p>
</div>
<form>
    <input type="button" name="moveLayer" value="move Left" onclick="move('left');">
    <input type="button" name="moveLayer" value="move Right" onclick="move('right');">
    <input type="button" name="moveLayer" value="move Up" onclick="move('up'); ">
    <input type="button" name="moveLayer" value="move Down" onclick="move('down'); ">
</form>

and the script is :

function move(direction) {
    var layerText = document.getElementByID("newtext");

    switch(direction) {
    
    case "left":
        layerText.style.left=50;
        break;
        
    case "right":
        layerText.style.right=150;
        break;
    
    case "up":
        layerText.style.up=50;
        break;
    case "down":
        layerText.style.down=150;
        break;
    default:
    it is not working;
    }
}

thanks in advance..

like image 611
melomane Avatar asked Feb 03 '26 06:02

melomane


2 Answers

This should work (there's no such thing as style.up and style.down):

function move(direction) {
    var layerText = document.getElementById("newtext");

    switch (direction) {

        case "left":
            layerText.style.left = "50px";
            break;

        case "right":
            layerText.style.left = "150px";
            break;

        case "up":
            layerText.style.top = "50px";
            break;

        case "down":
            layerText.style.top = "150px";
            break;
    }
}

Demo: http://jsfiddle.net/GE3C5/2/

like image 193
melancia Avatar answered Feb 04 '26 19:02

melancia


layerText.style.up=50;
                 ^

layerText.style.down=150;
                 ^

no property up and down. use top and bottom instead;

default:
    it is not working;

may cause syntax error

like image 44
ohmygirl Avatar answered Feb 04 '26 19:02

ohmygirl