dotkpay Posted December 22, 2011 Share Posted December 22, 2011 Hi, I need to call a function in my code but I want it to run inside a preloaded div on the page. How do I go about this, the div has an id. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/253658-function-in/ Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 <script>function();</script> Quote Link to comment https://forums.phpfreaks.com/topic/253658-function-in/#findComment-1300388 Share on other sites More sharing options...
dotkpay Posted December 22, 2011 Author Share Posted December 22, 2011 Not sure I got what you mean, But here is an example: <html> <head> <style type="text/css"> #area1 { position:absolute; top:100px; left:100px; } </style> </head> <body> <div> <script type="text/javascript"> function greet() { document.write("Hello, Good morning"); } </script> <button onclick="greet()">Click to greet</button> <div id="area1"></div> </div> </body> </html> Could you please show me how to make the greeting appear inside the div(area1) when I click the button. Quote Link to comment https://forums.phpfreaks.com/topic/253658-function-in/#findComment-1300392 Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 Like this? http://jsfiddle.net/VAwDt/ You probably want innerHTML instead of document.write(), since document.write() replaces the entire DOM. Quote Link to comment https://forums.phpfreaks.com/topic/253658-function-in/#findComment-1300397 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 22, 2011 Share Posted December 22, 2011 hope this helps <body> <div> <script type="text/javascript"> function greet() { var asd = document.getElementById['area1']; area1.innerHTML = "Hello, Good morning"; } </script> <button onclick="greet()">Click to greet</button> <div id="area1"></div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253658-function-in/#findComment-1300404 Share on other sites More sharing options...
trq Posted December 22, 2011 Share Posted December 22, 2011 You shouldn't mix your code into your markup. There are frameworks around these days that support unobtrusive JavaScript allowing you to attach events to elements by selecting those elements using CSS selectors. jQuery is probably the most popular of these frameworks, check it out. Quote Link to comment https://forums.phpfreaks.com/topic/253658-function-in/#findComment-1300407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.