Jump to content

onclick problem


Little Goat

Recommended Posts

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

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

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

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

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

Guest ravindra
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))

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

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 by Dan The Prof
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...