How to change an attribute, javascript

3

I'm trying to change the value of an attribute but apparently nothing changes, see the example used:

console.log(document.getElementById("stars").offsetTop); // me retorna 83
document.getElementById("stars").offsetTop = 10;
console.log(document.getElementById("stars").offsetTop); // continua retornando 83
    
asked by anonymous 14.01.2016 / 20:31

1 answer

1

Your method used as an example is that it is the problem, offsetTop is a get method, not set . That is, with it you can only get values and not declare them or change them. In this case, as offsetTop captures top of the element in question (in pixels), the maximum you can do to change it is through css :

document.getElementById("stars").style.top = valor
// O propriedade top só pode ser usada após a declaraçã o da propriedade position...

But you can change the "attributes" by javascript, for example with the scrollTop method. See in this example:

 console.log(document.getElementById("stars").scrollTop); // retornará 0
 document.getElementById("stars").scrollTop = 10;
 console.log(document.getElementById("stars").scrollTop); // retornará 10

The scrollTop yes method is a get/set method. With it you capture and determine values.

Demo - JsFiddle

    
14.01.2016 / 21:00