Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

getElementByID/parameters problem

Question posted by: anonymousstar (Newbie) on May 14th, 2008 09:53 AM
Hi All,

I have searched the forums to see if I can find an answer to my question but to no avail.

I have a form below with a 3 x 3 cell. Each of these cells change from green to yellow if selected and back to green if unselected. What I would like to do is when I press the continue button the cells I have clicked will have "selected" concatenated onto the ID name so that I can see which cells the user has selected.

I have set up an action class in java to show the parameter names in the tomcat log file "stdout" but it is not passing any parameters.

I realise that the problem is with the javascript and not sure how to dynamically use the elementbyid code so that it applies to any of the cells.

I hope I have made myself clear on the problem and you will be able to help.

Thanks

Anonymousstar

JAVASCRIPT

<script language="javascript">
Code: ( text )
  1. function changeColor(objTD)
  2. {   
  3.       if(objTD.clicked)
  4.       {
  5.             objTD.style.backgroundColor = "009933";
  6.             objTD.clicked=false;
  7.       }
  8.       else
  9.       {
  10.             objTD.style.backgroundColor="FFFF00";
  11.             objTD.clicked=true;
  12.       }
  13.       document.getElementById('td').value += " selected"
  14. }
</script>

HTML

Code: ( text )
  1. <p>
  2. <form name="grid">
  3. <table width="200" border="0" cellspacing="0" cellpadding="0">
  4.   <tr>
  5.     <td height="20" bgcolor="#009933" id="r1c1" onClick="changeColor(this)"><input type="hidden" id="r1c1" name="r1c1" value="1,1"></td>
  6.     <td height="20" bgcolor="#009933" id="r1c2" onClick="changeColor(this)"><input type="hidden" id="r1c2" name="r1c2" value="1,2"></td>
  7.     <td height="20" bgcolor="#009933" id="r1c3" onClick="changeColor(this)"><input type="hidden" id="r1c3" name="r1c3" value="1,3"></td>
  8.   </tr>
  9.   <tr>
  10.     <td height="20" bgcolor="#009933" id="r2c1" onClick="changeColor(this)"><input type="hidden" id="r2c1" name="r1c1" value="2,1"></td>
  11.     <td height="20" bgcolor="#009933" id="r2c2" onClick="changeColor(this)"><input type="hidden" id="r2c2" name="r1c2" value="2,2"></td>
  12.     <td height="20" bgcolor="#009933" id="r2c3" onClick="changeColor(this)"><input type="hidden" id="r2c3" name="r1c3" value="2,3"></td>
  13.   </tr>
  14.   <tr>
  15.     <td height="20" bgcolor="#009933" id="r3c1" onClick="changeColor(this)"><input type="hidden" id="r3c1" name="r3c1" value="3,1"></td>
  16.     <td height="20" bgcolor="#009933" id="r3c2" onClick="changeColor(this)"><input type="hidden" id="r3c2" name="r3c2" value="3,2"></td>
  17.     <td height="20" bgcolor="#009933" id="r3c3" onClick="changeColor(this)"><input type="hidden" id="r3c3" name="r3c3" value="3,3"></td>
  18.   </tr>
  19. </table>
  20. <a href="../do/continue2"><img src="../images/continue_button.gif" width="70" height="24" border="0" value="submit"/></a>
  21. </form>
  22. </p>
Last edited by acoder : May 14th, 2008 at 12:56 PM. Reason: Added code tags
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
hsriat's Avatar
hsriat
Expert
1,447 Posts
May 14th, 2008
10:03 AM
#2

Re: getElementByID/parameters problem
Code: ( text )
  1. var selected = null;
  2. function changeColor(objTD) {
  3.     selected = !selected ? objTD.id : null;
  4.     objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
  5. }

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 14th, 2008
10:27 AM
#3

Re: getElementByID/parameters problem
Quote:
Originally Posted by hsriat
Code: ( text )
  1. var selected = null;
  2. function changeColor(objTD) {
  3.     selected = !selected ? objTD.id : null;
  4.     objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
  5. }


Thanks for that, it changes colour which is great but it still is not passing the values i.e the cells that have been selected. I checked the "stdout" file but nothing. Anything else would be great or if it should work an explanation of the code would be useful as well.

Thanks

Reply
hsriat's Avatar
hsriat
Expert
1,447 Posts
May 14th, 2008
10:52 AM
#4

Re: getElementByID/parameters problem
Quote:
Originally Posted by anonymousstar
Thanks for that, it changes colour which is great but it still is not passing the values i.e the cells that have been selected. I checked the "stdout" file but nothing. Anything else would be great or if it should work an explanation of the code would be useful as well.

Thanks

use the value saved in the variable selected

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 14th, 2008
12:06 PM
#5

Re: getElementByID/parameters problem
Quote:
Originally Posted by hsriat
use the value saved in the variable selected


Do you mean

var selected = #FFFF00;

Reply
acoder's Avatar
acoder
Site Moderator
10,420 Posts
May 14th, 2008
12:59 PM
#6

Re: getElementByID/parameters problem
Firstly, you're using a link, not a submit button. Change the Continue link into a submit button.

Secondly, the ID is useful on the client side, but when submitting a form, you need the name, so change the name or even the value.

Finally, please use [code] tags when posting code. Thanks!

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 15th, 2008
10:44 AM
#7

Re: getElementByID/parameters problem
Hi Thanks for your advice and sorry about the code tags. I have done what you have suggested and I am getting all the parameters and their values which is great, but when I click on a single or multiple cells I am unable to identify which ones has been selected. I need to change the value or concatenate "selected" onto the value so I know which one I have clicked. I have tried numerous codes but to no avail.

One code I tried, looped through all the cells and changed the values of each cell starting at 0. I just wanted to change the one cell I had selected. Is there a way to modify the code below to achieve this?

Code: ( text )
  1. function changeVal(){
  2. var cells = document.getElementById('mytab').getElementsByTagN  ame('td');
  3. for(var i=0;i<cells.length;i++){
  4. cells[i].getElementsByTagName('input')[0].value=i;
  5. cells[i].onclick=function(){add(this.getElementsByTagName(  'input')[0])}
  6. }
  7. }


The code that I am using at the moment which does not change selected cells is below.

Code: ( text )
  1. <script language="javascript">
  2. var selected = null;
  3. function changeColor(objTD) {
  4.     selected = !selected ? objTD.id : null;
  5.     objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
  6. }
  7. </script>
  8.  
  9. <form action="../do/continue2">
  10. <table width="200" border="0" cellspacing="0" cellpadding="0" id="grid">
  11.   <tr>
  12.     <td height="20" bgcolor="#009933" id="r1c1" onClick="changeColor(this)"><input type="hidden" id="r1c1b" name="week1" value="row11"></td>
  13.     <td height="20" bgcolor="#009933" id="r1c2" onClick="changeColor(this)"><input type="hidden" id="r1c2b" name="week2" value="row12"></td>
  14.     <td height="20" bgcolor="#009933" id="r1c3" onClick="changeColor(this)"><input type="hidden" id="r1c3b" name="week3" value="row13"></td>
  15.   </tr>
  16.   <tr>
  17.     <td height="20" bgcolor="#009933" id="r2c1" onClick="changeColor(this)"><input type="hidden" id="r2c1b" name="week4" value="row21"></td>
  18.     <td height="20" bgcolor="#009933" id="r2c2" onClick="changeColor(this)"><input type="hidden" id="r2c2b" name="week5" value="row22"></td>
  19.     <td height="20" bgcolor="#009933" id="r2c3" onClick="changeColor(this)"><input type="hidden" id="r2c3b" name="week6" value="row23"></td>
  20.   </tr>
  21.   <tr>
  22.     <td height="20" bgcolor="#009933" id="r3c1" onClick="changeColor(this)"><input type="hidden" id="r3c1b" name="week7" value="row31"></td>
  23.     <td height="20" bgcolor="#009933" id="r3c2" onClick="changeColor(this)"><input type="hidden" id="r3c2b" name="week8" value="row32"></td>
  24.     <td height="20" bgcolor="#009933" id="r3c3" onClick="changeColor(this)"><input type="hidden" id="r3c3b" name="week9" value="row33"></td>
  25.   </tr>
  26. </table>
  27. <input type=submit value="Submit">
  28. </form>


I hope I am not annoying anyone and you dont get fed up with me.

Thanks

Last edited by anonymousstar : May 15th, 2008 at 10:44 AM. Reason: spelling
Reply
acoder's Avatar
acoder
Site Moderator
10,420 Posts
May 15th, 2008
12:04 PM
#8

Re: getElementByID/parameters problem
Change the value of the hidden field which will be passed to the server-side script in changeColor:
Code: ( text )
  1. function changeColor(objTD) {
  2.     selected = !selected ? objTD.id : null;
  3.     objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
  4.     var hiddenField = document.getElementById(objTD.id+"b");
  5.     hiddenField.value = !selected ? hiddenField.value.replace("selected","") : hiddenField.value += "selected";
  6. }

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 15th, 2008
03:11 PM
#9

Re: getElementByID/parameters problem
Quote:
Originally Posted by acoder
Change the value of the hidden field which will be passed to the server-side script in changeColor:
Code: ( text )
  1. function changeColor(objTD) {
  2.     selected = !selected ? objTD.id : null;
  3.     objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
  4.     var hiddenField = document.getElementById(objTD.id+"b");
  5.     hiddenField.value = !selected ? hiddenField.value.replace("selected","") : hiddenField.value += "selected";
  6. }


Thank you, I had to change the variable to 0 and swap the colours around so that every time it showed yellow it would say selected.


Thanks everyone for your help.

Kind Regards

Last edited by anonymousstar : May 15th, 2008 at 03:12 PM. Reason: spelling
Reply
acoder's Avatar
acoder
Site Moderator
10,420 Posts
May 16th, 2008
08:38 AM
#10

Re: getElementByID/parameters problem
You're welcome. Glad it's working.

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 16th, 2008
02:42 PM
#11

Re: getElementByID/parameters problem
Quote:
Originally Posted by acoder
You're welcome. Glad it's working.


I have one last question... sorry. the code is working fine but it takes a lot of clicks to change the cell from green to yellow and I need it to be one click.

does it have something to do with thes part of the code?

Code: ( text )
  1. !!selected ?


whole line of code:

Code: ( text )
  1. var selected = 0;
  2. objTD.style.backgroundColor = !!selected ? "#FFFF00" : "#009933";


Thanks in advance.

Reply
hsriat's Avatar
hsriat
Expert
1,447 Posts
May 16th, 2008
02:57 PM
#12

Re: getElementByID/parameters problem
Quote:
Originally Posted by anonymousstar
I have one last question... sorry. the code is working fine but it takes a lot of clicks to change the cell from green to yellow and I need it to be one click


If it takes lots of click, then perhaps there's time to buy a new mouse.
But if takes 2 clicks for the first time, then change !!selected to !selected or change the order of colors like this.
Code: ( text )
  1. objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";

Reply
acoder's Avatar
acoder
Site Moderator
10,420 Posts
May 16th, 2008
03:16 PM
#13

Re: getElementByID/parameters problem
Quote:
Originally Posted by anonymousstar
I have one last question... sorry. the code is working fine but it takes a lot of clicks to change the cell from green to yellow and I need it to be one click.
Oh, I didn't test the code. I just assumed that it worked.

Use the hiddenField value. If it contains "selected", change the color and vice versa, e.g.
Code: ( text )
  1. objTD.style.backgroundColor = (hiddenField.value.indexOf("selected"))? "#FFFF00" : "#009933";

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 16th, 2008
03:16 PM
#14

Re: getElementByID/parameters problem
Quote:
Originally Posted by hsriat
If it takes lots of click, then perhaps there's time to buy a new mouse.
But if takes 2 clicks for the first time, then change !!selected to !selected or change the order of colors like this.
Code: ( text )
  1. objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";



LOL. I did think it could be my mouse but the first cell changes fine with one click and goes back to green when I click it again. The problem is when I want to click another cell in the same row to yellow as well. I would need to click a maximum of 4 cells left or right at a time.

Reply
hsriat's Avatar
hsriat
Expert
1,447 Posts
May 16th, 2008
04:02 PM
#15

Re: getElementByID/parameters problem
There were few bugs in your code. Try this out...
Code: ( text )
  1. <script type="text/javascript">
  2. function changeColor(objTD) {
  3.     var hiddenField = objTD.firstChild;
  4.     var selected = !hiddenField.value.match("selected");
  5.     objTD.style.backgroundColor = selected ? "#ffff00" : "#009933";
  6.     hiddenField.value = selected ? hiddenField.value + " selected" : hiddenField.value.replace(" selected","");
  7. }
  8. </script>
  9. <style type="text/css">
  10. table tr td {
  11.     background-color:#009933;
  12.     cursor:pointer;
  13.     height:20px;
  14. }
  15. </style>
  16. <form name="grid" action="../do/continue2" method="post">
  17. <table width="200" border="2" cellspacing="2" cellpadding="2">
  18.   <tr>
  19.     <td onclick="changeColor(this)"><input type="hidden" name="r1c1" value="1,1"></td>
  20.     <td onclick="changeColor(this)"><input type="hidden" name="r1c2" value="1,2"></td>
  21.     <td onclick="changeColor(this)"><input type="hidden" name="r1c3" value="1,3"></td>
  22.   </tr>
  23.   <tr>
  24.     <td onclick="changeColor(this)"><input type="hidden" name="r2c1" value="2,1"></td>
  25.     <td onclick="changeColor(this)"><input type="hidden" name="r2c2" value="2,2"></td>
  26.     <td onclick="changeColor(this)"><input type="hidden" name="r2c3" value="2,3"></td>
  27.   </tr>
  28.   <tr>
  29.     <td onclick="changeColor(this)"><input type="hidden" name="r3c1" value="3,1"></td>
  30.     <td onclick="changeColor(this)"><input type="hidden" name="r3c2" value="3,2"></td>
  31.     <td onclick="changeColor(this)"><input type="hidden" name="r3c3" value="3,3"></td>
  32.   </tr>
  33. </table>
  34. <input type="submit" value="Submit" name="Submit"/>
  35. </form>

Reply
anonymousstar's Avatar
anonymousstar
Newbie
12 Posts
May 19th, 2008
07:51 AM
#16

Re: getElementByID/parameters problem
Quote:
Originally Posted by hsriat
There were few bugs in your code. Try this out...
Code: ( text )
  1. <script type="text/javascript">
  2. function changeColor(objTD) {
  3.     var hiddenField = objTD.firstChild;
  4.     var selected = !hiddenField.value.match("selected");
  5.     objTD.style.backgroundColor = selected ? "#ffff00" : "#009933";
  6.     hiddenField.value = selected ? hiddenField.value + " selected" : hiddenField.value.replace(" selected","");
  7. }
  8. </script>
  9. <style type="text/css">
  10. table tr td {
  11.     background-color:#009933;
  12.     cursor:pointer;
  13.     height:20px;
  14. }
  15. </style>
  16. <form name="grid" action="../do/continue2" method="post">
  17. <table width="200" border="2" cellspacing="2" cellpadding="2">
  18.   <tr>
  19.     <td onclick="changeColor(this)"><input type="hidden" name="r1c1" value="1,1"></td>
  20.     <td onclick="changeColor(this)"><input type="hidden" name="r1c2" value="1,2"></td>
  21.     <td onclick="changeColor(this)"><input type="hidden" name="r1c3" value="1,3"></td>
  22.   </tr>
  23.   <tr>
  24.     <td onclick="changeColor(this)"><input type="hidden" name="r2c1" value="2,1"></td>
  25.     <td onclick="changeColor(this)"><input type="hidden" name="r2c2" value="2,2"></td>
  26.     <td onclick="changeColor(this)"><input type="hidden" name="r2c3" value="2,3"></td>
  27.   </tr>
  28.   <tr>
  29.     <td onclick="changeColor(this)"><input type="hidden" name="r3c1" value="3,1"></td>
  30.     <td onclick="changeColor(this)"><input type="hidden" name="r3c2" value="3,2"></td>
  31.     <td onclick="changeColor(this)"><input type="hidden" name="r3c3" value="3,3"></td>
  32.   </tr>
  33. </table>
  34. <input type="submit" value="Submit" name="Submit"/>
  35. </form>


Fantastic works a treat. Thank you both for your help and time it is much appreciated.

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,097 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Javascript / DHTML / Ajax Forum Contributors