fortnox007 Posted September 17, 2010 Share Posted September 17, 2010 Hi all Sorry in advance for my ignorance, but I just started learning java script today. I wrote a little script with a condition. If the condition is true the stuff inside a <p> element should be altered, but For some reason I can't see how to do this I looked up for the DOM, but i didn't find <p> in there. (is it even possible?) If someone maybe has an idea on how to achieve this (or maybe a nice alternative)please let me know, All tips and hints are welcome. (I ordered a book today, so I'll post less noobish scripts in the future) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JAVA SCRIPT TEST PAGE</title> <script type="text/javascript"> function my_first_function(){ var text1 = 'monkeys'; if (document.getElementById(input)!= text1){ document.write(document.getElementById(p_element)='wrong text'); }else{ document.write(document.getElementById(p_element)='your correct'); } } </script> </head> <body> <input type="text" size="30" id="input" onchange="my_first_function()" value="" /> <br /> <p id="p_element">This is just default text.</p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/ Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 Ah it seems i need innerHtml, lol well if someone knows more I would love to hear it Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/#findComment-1112257 Share on other sites More sharing options...
Adam Posted September 17, 2010 Share Posted September 17, 2010 Not far off.. function my_first_function(){ var text1 = 'monkeys'; if (document.getElementById('input').value != text1){ document.getElementById('p_element').innerHTML = 'wrong text'; }else{ document.getElementById('p_element').innerHTML = 'your correct'; } } Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/#findComment-1112268 Share on other sites More sharing options...
Psycho Posted September 17, 2010 Share Posted September 17, 2010 There's one other error document.getElementById('p_element').innerHTML = 'your correct'; Should be document.getElementById('p_element').innerHTML = "you're correct"; Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/#findComment-1112271 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 Oh wow! ty guys, I have been searching my butt off. May I Ask 1 more thing. I have the feeling if I use document.write(); the page refreshes is that true? I haven't found other ways of printing text besides that and the innerHtml now : ) Anyways thanks alot and sorry for the noobie script I wrote. -edit: Yeah i think document.write really refreshes the page. I have please an extra piece in the function to test document.write('lalalalalaa'); When this gets executed the page turns blank and tels lalalalalaa (hmm maybe that's because the function is in between the header) Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/#findComment-1112273 Share on other sites More sharing options...
Adam Posted September 17, 2010 Share Posted September 17, 2010 If called in-line, it will just write the string where it's called. Behaviour varies when called from an event though. It doesn't actually refresh the page, it just overwrites the content. Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/#findComment-1112279 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 I had that feeling. Cool Thanks alot for helping ;-) I just changed onchange() for onkeyup() and its now super responsive, really awesome. Quote Link to comment https://forums.phpfreaks.com/topic/213690-my-first-javascript-function-pllease-dont-laugh/#findComment-1112280 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.