niche Posted September 6, 2011 Share Posted September 6, 2011 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 More sharing options...
justsomeguy Posted September 6, 2011 Share Posted September 6, 2011 If you want to display the style:alert(div.getAttribute('style')); Link to comment Share on other sites More sharing options...
niche Posted September 6, 2011 Author Share Posted September 6, 2011 How would I target the div with the id=mydiv2 in the alert? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 6, 2011 Share Posted September 6, 2011 Use document.getElementById. Link to comment Share on other sites More sharing options...
danman97 Posted September 6, 2011 Share Posted September 6, 2011 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 More sharing options...
niche Posted September 6, 2011 Author Share Posted September 6, 2011 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 More sharing options...
Drycodez Posted September 6, 2011 Share Posted September 6, 2011 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? That is the shortest! Link to comment Share on other sites More sharing options...
niche Posted September 6, 2011 Author Share Posted September 6, 2011 This is very informative topic. Many thanks to justsomeguy, danman97 and O. Samuel. Link to comment Share on other sites More sharing options...
danman97 Posted September 6, 2011 Share Posted September 6, 2011 Your Welcome... Even though my method didn't work. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 7, 2011 Share Posted September 7, 2011 Your Welcome... Even though my method didn't work. 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 More sharing options...
niche Posted September 7, 2011 Author Share Posted September 7, 2011 I love your follow-ups shadowMage. We always benefit from a double check of a topic. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.