1

I have some code that is roughly along the lines of this:

exportValue = []; 

function reduceArray() {
    //does something 
    exportValue = parseFloat(exportValue)
}

From that, I get that exportValue is 73951. I then have to add that number to the page... so I tried both of these:

$("#exportValueDiv").append(exportValue);
$("#exportValueDiv").append("<li>" + exportValue + "</li>");

But that doesn't work.. I'm confused on how to add something like a variable to the DOM....

If I do something like:

$( "#exportValueDiv" ).append( "<li>value</li>") 

it works, but I don't want to add a string, I want to add the value of the variable. I looked this up, but I'm still confused, so any help would be greatly appreciated!!!

1
  • 3
    jQuery's append() method (as you're using it) appends an HTML element (identified by a string containing HTML, a wrapped jQuery element or an actual element object), not "text". If you want to simply set the content of your div to the value try $("#exportValueDiv").text(exportValue); Commented Feb 18, 2016 at 0:31

4 Answers 4

4

Look into jQuery manipulation

$("#exportValueDiv").text(exportValue); //Replaces text of #exportValueDiv
$("#exportValueDiv").html('<span>'+exportValue+'</span>'); //Replaces inner html of #exportValueDiv
$("#exportValueDiv").append('<span>'+exportValue+'</span>'); //Adds to the inner html of #exportValueDiv
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation! But I figured out my error was due to an unfinished AJAX call
2

The .append() contract expects a DOM element or HTML String. You will need to do:

$("#exportValueDiv").append("<div>" + exportValue + "</div>");

2 Comments

He spent the time to link to documentation.
Thanks for the answer!
1

Try this:

$("#exportValueDiv").append("<div>" + exportValue + "</div>");

1 Comment

Thanks for the answer!
1

The following appends your variable to a div that already has information:

<div id="exportValueDiv">
<p>
Some information.
</p>
</div>

<script>
var exportValue = "Hello world.";
$("#exportValueDiv").append('<p>'+ exportValue +'</p>');
</script>

https://jsfiddle.net/supadave57/f9tqw0d4/

1 Comment

Thanks for the answer!!

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.