Little Goat Posted May 9, 2006 Share Posted May 9, 2006 hi,I have a problem with this code: <script type="text/javascript">document.getElementByTagName('td').onclick = new Function("this.innerHTML = prompt('please enter a value')");</script> I have no Idea how to use the "new Function()" thing, so I could be completely wrong.can anyone tell me what the problem is?thnx, LG(so much for mastering javascript. (see sig)) Link to comment Share on other sites More sharing options...
skym Posted May 9, 2006 Share Posted May 9, 2006 I am not a JS master either, but I think that: var popup = new Function("txt","alert(txt)") is echivalent to: function popup(txt){ alert(txt)} Link to comment Share on other sites More sharing options...
Little Goat Posted May 9, 2006 Author Share Posted May 9, 2006 yea that is what I thought, but my main problem is setting the onmousedown or onclick of all the <td> tags.ok, so say I had this: function changeval(obj){obj.innerHTML = prompt('please enter a number');} then all I need is to set all <td> tags' onmousedown to changeval(this)can anyone help plez?thnx, LG Link to comment Share on other sites More sharing options...
Chocolate570 Posted May 9, 2006 Share Posted May 9, 2006 Try this. <script type="text/javascript">document.getElementsByTagName('td')[0].onclick = function lol(){("this.innerHTML = prompt('please enter a value')")};</script> Link to comment Share on other sites More sharing options...
Little Goat Posted May 9, 2006 Author Share Posted May 9, 2006 thanks, but that only does one, Right?I need all of the TDs to be set to that.do I need to use a while loop?LGEidt:this still doesn't work. I tried just as you said, and this: document.getElementsByTagName('td')[0].onclick = lol();function lol(){this.innerHTML = prompt('please enter a value')} Link to comment Share on other sites More sharing options...
Chocolate570 Posted May 9, 2006 Share Posted May 9, 2006 Ok, this should do what you want. function lol(){this.innerHTML = prompt('please enter a value')}tdCol=document.getElementsByTagName("td");for(i=0;i<=tdCol.length;i++) {tdCol[i].onclick = lol;} Link to comment Share on other sites More sharing options...
Little Goat Posted May 10, 2006 Author Share Posted May 10, 2006 that doesn't work, but when I add the attribute to the td it shows the prompt, but doesn't change the value.??????LG Link to comment Share on other sites More sharing options...
skym Posted May 10, 2006 Share Posted May 10, 2006 It works at me too. There is an error in the 'for' line, might help. Use:for(i=0;i<tdCol.length;i++) { Link to comment Share on other sites More sharing options...
Guest ravindra Posted May 10, 2006 Share Posted May 10, 2006 hi,I have a problem with this code:<script type="text/javascript">document.getElementByTagName('td').onclick = new Function("this.innerHTML = prompt('please enter a value')");</script> I have no Idea how to use the "new Function()" thing, so I could be completely wrong.can anyone tell me what the problem is?thnx, LG(so much for mastering javascript. (see sig)) <{POST_SNAPBACK}> hi!!use this code, how to use a function in java script!!my function name=ravindra()<html><head><script type="text/javascript">//function name ravindrafunction ravindra(){var name=prompt("Please enter your name","")if (name!=null && name!=""){document.write("Hello " + name + "! How are you today?")}}</script></head><body><form><input type="button" onclick="ravindra()" value="Prompt box"></form></body></html> Link to comment Share on other sites More sharing options...
Jack McKalling Posted May 10, 2006 Share Posted May 10, 2006 (edited) If you do not want to use script elements, it can also be done like this:<td onclick='function(){Answer = prompt("question","answer")}'>Click on this cell</td>(to use Answer as a variable elsewere) Edited May 10, 2006 by Dan The Prof Link to comment Share on other sites More sharing options...
Little Goat Posted May 10, 2006 Author Share Posted May 10, 2006 ok, thanks for the help everyone, I know about the attributes, but I wanted to set all the attributes in the script, as I have a lot of <td>s and I might add more.anyway, this function works fine changing it and all when I use an ATTRIBUTE: function lol(obj){var value = prompt('please enter a value')obj.innerHTML = value;} the attribute is onclick="lol(this)"but I would still like to set the onclick inside the script.????thnx for your help.LG Link to comment Share on other sites More sharing options...
skym Posted May 10, 2006 Share Posted May 10, 2006 Chocolate's code does work, you just have to put the script after the TDs, the following code works at me: <html><head><title>title</title></head><body><table border="1"><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr></table><script type="text/javascript">function lol(){this.innerHTML = prompt('please enter a value')}tdCol=document.getElementsByTagName("td");for(i=0;i<tdCol.length;i++) {tdCol[i].onclick = lol;}</script></body></html> Link to comment Share on other sites More sharing options...
Little Goat Posted May 10, 2006 Author Share Posted May 10, 2006 oh, no wonder. why didn't I realize that?thnx, it works perfect.LG Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now