How to get innerHTML from a div?
Asked Answered
D

2

7

I need to save the innerHTML of a div and store it in a cookie. Here is my basic code.

//The div-saving function
function addStatus(sName){
    getElement("bottomDiv").innerHTML += sName + "<br>";
    document.cookie="status=" + sName + "<br>";
}

//The button clicking function
function clk(){
    num++;
    document.cookie = "num=" + num;
    switch(num){
        case 25:
            addStatus("25: Alright! Let the games begin!");
        break;
        case 50:
            addStatus("50: Woah! Getting into the high numbers!");
        break;
    }
}

What ends up happening is only the last achieved status gets saved. I need to know how to get the innerHTML of the div instead. Thank you.

Drinking answered 21/4, 2014 at 4:34 Comment(5)
does your div have a class or id?Beniamino
Please change the line: document.cookie = "status"+document.getElementById("bottomDiv").innerHTML+"</br>"Lymphatic
Paste your html code here jsfiddle.netLymphatic
document.cookie="status=" + document.getElementById("bottomDiv").innerHTML+ "<br>";Consul
Here it is the link for jsfiddle. For some reason some stuff is broken dont know why it works perfectly on my local server. jsfiddle.net/Txdt4Drinking
G
6

Try this..

 var div= document.getElementById("bottomDiv").innerHTML;
Germinal answered 21/4, 2014 at 4:38 Comment(0)
J
1
var holder = document.getElementById("bottomDiv").innerHTML;

The above will get the innerHTML value and assign it to the variable. Also there is no built in function getElement , you can use getElementById, there are other variations as well.

Juggins answered 21/4, 2014 at 4:40 Comment(1)
I know there is no built in function getElement. I forget to mention I made that function.Drinking

© 2022 - 2024 — McMap. All rights reserved.