I keep getting undefined with this script...

I

document.getElementById(new_id).style.top = Top; //Works Fine
document.getElementById(new_id).style.left = Left; //Works Fine
document.getElementById(new_id).style.backgroundColor = Background_Color; //Works Fine
document.getElementById(new_id).style.border = Border_Width+" "+Border_Type+" "+Border_Color; //Works Fine
        
new_code = document.getElementById(new_id); //Doesn't Work!!!!
        
document.write(new_code.value);

I style the element correctly, but when I want to write it I keep getting "undefined"?

Member Avatar for stbuchok

When you do alert(document.getElementById(new_id) just before line 1 and before line 5, what are the 2 values that come up?

It depends on the type of element. Are you sure your element has a .value property?

.value on a div will give undefined, where .value on a textbox will give you the value.

I have a div not a textbox and I still am using .value. Plus when I alert it without .value I get [object HTML DivElement] both times.

What do I need to do to fix it? So I can't use .value? What do I use to echo the entire div including <div style="...">...Content...</div> all of that?

So basically all I want to do is get all of that div...

<div style="...">...Content...</div>

And place it inside a variable...

Try using innerHTML, and remove .value property on "new_code":

var new_code = document.getElementById(new_id)[B].innerHTML[/B];
document.write(new_code);

I fixed it I added a new div around it and then called innerHTML thanks everyone!

<div id="div1">
     <div id="div2"></div>
</div>

//Javascript

var new_code = document.getElementById("div1").innerHTML;

After doing that I get all of div2!

Please mark this thread as Solved if your problem was solved. It will make use clear that there is no need to answer in this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.