Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  200] [ 7]  / answers: 1 / hits: 23466  / 13 Years ago, fri, june 10, 2011, 12:00:00

I've got a little bit of javascript embedded in my html (using a .aspx file). I want to perform some sort of if statement which then determines whether or not some sort of chart is displayed. This chart is displayed using html, and I'm assuming the if statement should be written in javascript. However, I don't really know how to run this html code from within java. It is basically just drawing a table. Any suggestions? I've seen document.write, but I've only seen that being used with single lines.


More From » .net

 Answers
84

You don't really run an HTML code. HTML is a markup language and it is mostly used to format and arrange elements that are displayed in the web browser.



The problem you are probably trying to solve is: Display or hide an element based on some condition. A JavaScript code like this is what you want.



if (condition) {
document.getElementById('chart').style.display = none
} else {
document.getElementById('chart').style.display =
}


Of course whatever element is responsible for displaying the chart should have an id=chart attribute. e.g. <div id=chart><!-- Chart related code goes here --></div>.



The JavaScript code I have given alters the display CSS property of this element to hide it or make it visible.



In case this is not what you want but you want to dynamically modify the HTML responsible for the chart, then you need to use the innerHTML property of the element.



Example:



if (condition) {
document.getElementById('chart').innerHTML = <!-- HTML Code for the chart here -->
} else {
document.getElementById('chart').innerHTML =
}

[#91768] Thursday, June 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckenna

Total Points: 445
Total Questions: 109
Total Answers: 109

Location: Virgin Islands (U.S.)
Member since Sun, May 16, 2021
3 Years ago
;