5

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.

3
  • Show us what you have so far. By the sound of things you may need to us element.innerHTML. Commented Jun 10, 2011 at 15:31
  • See this link stackoverflow.com/questions/962173/using-javascript-to-add-html There are tons of examples elsewhere, google "add html with javascript" Commented Jun 10, 2011 at 15:31
  • 1
    Please provide more info like code samples etc. Commented Jun 10, 2011 at 15:32

3 Answers 3

4

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 = ""
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming the if statement should be written in javascript

Unless you are testing something that you can only find out out in JS, then do it server side in your ASP.NET code.

I don't really know how to "run" this html code from within

This is covered by chapter 47 of Opera's WSC: Creating and modifying HTML. You may wish to read some of the earlier chapters first.

java

Java has about as much in common with JavaScript as Car does with Carpet. They are completely different programming languages.

Comments

0

Try writing the if statement in javascript and then rendering the html with the JQuery html() function. Just use a custom html id to locate where you want the html code to go.

<div id = "custom-tag"> </div>


<script>
    if (true){
        $('#custom-tag').html('YourHtmlString');
    } else {
        $('#custom-tag').html('DifferentHtmlString');
    }
</script>

Read more about html() here: http://api.jquery.com/html/

YourHtmlString & DifferentHtmlString is where you should store your custom html in string format. It will then be rendered wherever you included "div id = 'custom-id'

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.