473,503 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getElementByID/parameters problem

15 New Member
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">
Expand|Select|Wrap|Line Numbers
  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. }
  15.  
</script>

HTML

[HTML]<p>
<form name="grid">
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="20" bgcolor="#009933" id="r1c1" onClick="changeColor(this)"><input type="hidden" id="r1c1" name="r1c1" value="1,1"></td>
<td height="20" bgcolor="#009933" id="r1c2" onClick="changeColor(this)"><input type="hidden" id="r1c2" name="r1c2" value="1,2"></td>
<td height="20" bgcolor="#009933" id="r1c3" onClick="changeColor(this)"><input type="hidden" id="r1c3" name="r1c3" value="1,3"></td>
</tr>
<tr>
<td height="20" bgcolor="#009933" id="r2c1" onClick="changeColor(this)"><input type="hidden" id="r2c1" name="r1c1" value="2,1"></td>
<td height="20" bgcolor="#009933" id="r2c2" onClick="changeColor(this)"><input type="hidden" id="r2c2" name="r1c2" value="2,2"></td>
<td height="20" bgcolor="#009933" id="r2c3" onClick="changeColor(this)"><input type="hidden" id="r2c3" name="r1c3" value="2,3"></td>
</tr>
<tr>
<td height="20" bgcolor="#009933" id="r3c1" onClick="changeColor(this)"><input type="hidden" id="r3c1" name="r3c1" value="3,1"></td>
<td height="20" bgcolor="#009933" id="r3c2" onClick="changeColor(this)"><input type="hidden" id="r3c2" name="r3c2" value="3,2"></td>
<td height="20" bgcolor="#009933" id="r3c3" onClick="changeColor(this)"><input type="hidden" id="r3c3" name="r3c3" value="3,3"></td>
</tr>
</table>
<a href="../do/continue2"><img src="../images/continue_button.gif" width="70" height="24" border="0" value="submit"/></a>
</form>
</p>[/HTML]
May 14 '08 #1
15 1985
hsriat
1,654 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. var selected = null;
  2. function changeColor(objTD) {
  3.     selected = !selected ? objTD.id : null;
  4.     objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
  5. }
May 14 '08 #2
anonymousstar
15 New Member
Expand|Select|Wrap|Line Numbers
  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
May 14 '08 #3
hsriat
1,654 Recognized Expert Top Contributor
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
May 14 '08 #4
anonymousstar
15 New Member
use the value saved in the variable selected
Do you mean

var selected = #FFFF00;
May 14 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
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!
May 14 '08 #6
anonymousstar
15 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. function changeVal(){
  2. var cells = document.getElementById('mytab').getElementsByTagName('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.

Expand|Select|Wrap|Line Numbers
  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
May 15 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
Change the value of the hidden field which will be passed to the server-side script in changeColor:
Expand|Select|Wrap|Line Numbers
  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. }
May 15 '08 #8
anonymousstar
15 New Member
Change the value of the hidden field which will be passed to the server-side script in changeColor:
Expand|Select|Wrap|Line Numbers
  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
May 15 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
You're welcome. Glad it's working.
May 16 '08 #10
anonymousstar
15 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. !!selected ? 
whole line of code:

Expand|Select|Wrap|Line Numbers
  1. var selected = 0;
  2. objTD.style.backgroundColor = !!selected ? "#FFFF00" : "#009933";
Thanks in advance.
May 16 '08 #11
hsriat
1,654 Recognized Expert Top Contributor
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.
Expand|Select|Wrap|Line Numbers
  1. objTD.style.backgroundColor = !!selected ? "#009933" : "#FFFF00";
May 16 '08 #12
acoder
16,027 Recognized Expert Moderator MVP
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.
Expand|Select|Wrap|Line Numbers
  1. objTD.style.backgroundColor = (hiddenField.value.indexOf("selected"))? "#FFFF00" : "#009933";
May 16 '08 #13
anonymousstar
15 New Member
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.
Expand|Select|Wrap|Line Numbers
  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.
May 16 '08 #14
hsriat
1,654 Recognized Expert Top Contributor
There were few bugs in your code. Try this out...[html]<script type="text/javascript">
function changeColor(objTD) {
var hiddenField = objTD.firstChild;
var selected = !hiddenField.value.match("selected");
objTD.style.backgroundColor = selected ? "#ffff00" : "#009933";
hiddenField.value = selected ? hiddenField.value + " selected" : hiddenField.value.replace(" selected","");
}
</script>
<style type="text/css">
table tr td {
background-color:#009933;
cursor:pointer;
height:20px;
}
</style>
<form name="grid" action="../do/continue2" method="post">
<table width="200" border="2" cellspacing="2" cellpadding="2">
<tr>
<td onclick="changeColor(this)"><input type="hidden" name="r1c1" value="1,1"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r1c2" value="1,2"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r1c3" value="1,3"></td>
</tr>
<tr>
<td onclick="changeColor(this)"><input type="hidden" name="r2c1" value="2,1"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r2c2" value="2,2"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r2c3" value="2,3"></td>
</tr>
<tr>
<td onclick="changeColor(this)"><input type="hidden" name="r3c1" value="3,1"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r3c2" value="3,2"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r3c3" value="3,3"></td>
</tr>
</table>
<input type="submit" value="Submit" name="Submit"/>
</form>[/html]
May 16 '08 #15
anonymousstar
15 New Member
There were few bugs in your code. Try this out...[html]<script type="text/javascript">
function changeColor(objTD) {
var hiddenField = objTD.firstChild;
var selected = !hiddenField.value.match("selected");
objTD.style.backgroundColor = selected ? "#ffff00" : "#009933";
hiddenField.value = selected ? hiddenField.value + " selected" : hiddenField.value.replace(" selected","");
}
</script>
<style type="text/css">
table tr td {
background-color:#009933;
cursor:pointer;
height:20px;
}
</style>
<form name="grid" action="../do/continue2" method="post">
<table width="200" border="2" cellspacing="2" cellpadding="2">
<tr>
<td onclick="changeColor(this)"><input type="hidden" name="r1c1" value="1,1"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r1c2" value="1,2"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r1c3" value="1,3"></td>
</tr>
<tr>
<td onclick="changeColor(this)"><input type="hidden" name="r2c1" value="2,1"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r2c2" value="2,2"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r2c3" value="2,3"></td>
</tr>
<tr>
<td onclick="changeColor(this)"><input type="hidden" name="r3c1" value="3,1"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r3c2" value="3,2"></td>
<td onclick="changeColor(this)"><input type="hidden" name="r3c3" value="3,3"></td>
</tr>
</table>
<input type="submit" value="Submit" name="Submit"/>
</form>[/html]
Fantastic works a treat. Thank you both for your help and time it is much appreciated.
May 19 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
20911
by: Gary Mayor | last post by:
Hi, I'm back again. Basically i'm trying to draw a box over an image which is turning out to be a nightmare. The problem i'm getting at the moment is that i'm creating a line with <div which works...
7
1333
by: Gerry | last post by:
Hi, I have a javascript function which uses the method document.getElementById. I'm using it to decide whether a checkbox has been ticked or not. This decision is encoded in an if statement...
12
3867
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
5440
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
10
2650
by: JJA | last post by:
I'm trying to use document.getElementByID inside a function where the ID is passed as an argument. I get the same error ("Element has no properties") on the same statement inside the commonCheck...
20
6929
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
1
36504
by: Andre Ranieri | last post by:
Hello, I'm trying to set up an ASP.NET 2.0 form where the user enters values in WebControls.TextBoxes for amount owing, interest and late fees and a JavaScript function totals the three values...
9
2993
by: jason.hau | last post by:
Ok, interesting problem here, I have a webcontrol that holds a textbox and a requiredfieldvalidator from System.Web.UI.Webcontrols in .NET 2.0 (this is javascript related, bear with me). The...
4
12733
by: Claudio Calboni | last post by:
Hello folks, I'm having some performance issues with the client-side part of my application. Basically, it renders a huge HTML table (about 20'000 cells in my testing scenario), without content....
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.