Frank DTank De Leon
Frank DTank De Leon

Reputation: 11

How to set up dynamic style property to an object property

Is there anyway I can set up a style property dynamically to an objects property? I tried the code below and got an error.

var page = {showElement:'none'};

var markup='<div id="button"> </div><style>#button{display:'+page.showElement+';}</style>';

var mainDiv = document.getElementById('mainDiv');
    mainDiv.innerHTML = markup; 

Upvotes: 0

Views: 68

Answers (1)

Koby Douek
Koby Douek

Reputation: 16675

You already have an ID for your div element, why not use it to change the display property?

document.getElementById('button').style.diplay = 'none'; 

or

document.getElementById('button').style.diplay = page.showElement; 

So you dont really need to use:

mainDiv.innerHTML

Upvotes: 1

Related Questions