Jump to content

alert notation


niche

Recommended Posts

How should the alert be written if all I wanted displayed was the <div> and its style based on this function? alert(document.body.div); displayed as undefined.

var div = document.createElement('div');div.setAttribute("style","float:right;width:200px;height:100px;background-color:red");div.id='mydiv2';var txt='hello world!';document.getElementsByTagName('body')[0].appendChild(div);document.getElementById('mydiv2').innerHTML=txt;alert(document.body.innerHTML);}

Link to comment
Share on other sites

Edit: Didnt see that post above!Hmm... Could you usealert(div#mydiv2.getAttribute('style'));or maybe create it as a variable likealert(myvar.getAttribute('style'));Dont take my word for it because I am quite new to Javascript ;P

Link to comment
Share on other sites

Thanks for your post danman97, but neither of those alerts worked.I think I'm get the hang of this. This alert worked: alert(document.getElementById('mydiv2').getAttribute('style'));Is there a shorter notation that could be used?

Link to comment
Share on other sites

Your Welcome... Even though my method didn't work. :P
The second one would have worked if you first assigned the DOM element to myvar before using it in the alert:
var myvar = document.getElementById('mydiv2');alert(myvar.getAttribute('style'));

Link to comment
Share on other sites

I love your follow-ups shadowMage. We always benefit from a double check of a topic.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...