473,657 Members | 2,540 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="javas cript">
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="#00993 3" id="r1c1" onClick="change Color(this)"><i nput type="hidden" id="r1c1" name="r1c1" value="1,1"></td>
<td height="20" bgcolor="#00993 3" id="r1c2" onClick="change Color(this)"><i nput type="hidden" id="r1c2" name="r1c2" value="1,2"></td>
<td height="20" bgcolor="#00993 3" id="r1c3" onClick="change Color(this)"><i nput type="hidden" id="r1c3" name="r1c3" value="1,3"></td>
</tr>
<tr>
<td height="20" bgcolor="#00993 3" id="r2c1" onClick="change Color(this)"><i nput type="hidden" id="r2c1" name="r1c1" value="2,1"></td>
<td height="20" bgcolor="#00993 3" id="r2c2" onClick="change Color(this)"><i nput type="hidden" id="r2c2" name="r1c2" value="2,2"></td>
<td height="20" bgcolor="#00993 3" id="r2c3" onClick="change Color(this)"><i nput type="hidden" id="r2c3" name="r1c3" value="2,3"></td>
</tr>
<tr>
<td height="20" bgcolor="#00993 3" id="r3c1" onClick="change Color(this)"><i nput type="hidden" id="r3c1" name="r3c1" value="3,1"></td>
<td height="20" bgcolor="#00993 3" id="r3c2" onClick="change Color(this)"><i nput type="hidden" id="r3c2" name="r3c2" value="3,2"></td>
<td height="20" bgcolor="#00993 3" id="r3c3" onClick="change Color(this)"><i nput 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 2000
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

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

Similar topics

2
20929
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 when it's not hidden but I need to be able to make it hidden so I can use layers to show all when it finished drawing to make it smoother. This is how some other scripts are doing it that i've seen. So i've got it drawing a line and I need...
7
1340
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 with the condtion being if (document.getElementById(checkboxtoupdate).value ==1)
12
3904
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 both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById('weblogs').style.visibility='hidden';
4
5463
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) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
10
2675
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 function with either MS IE 6.0 or Mozilla Firefox 0.9. Comments below indicate that if I hardcode an element ID I can avoid the error, but I obviously want to parameterize it. Here are some snippets of my code: -----snippet...
20
6957
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
1
36515
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 and sets the value of a label (lblTotal) to the sum of the three textbox values. The form is in a content place holder. I've added the following block of code in my form load.
9
3007
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 validator is added to the page validator collection so at run time, ..NET auto generates an array e.g.: var Page_Validators = new Array(document.getElementById("ctl0_ContentPlaceHolder1_UcControl1__ctl1"),...
4
12752
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. Content is "pushed" from the back via some JS, for the only displayed portion of table. Once the user scrolls, JS updates visible cells with data. It's quite the philosophy behind GMaps and similars. So, the server says to JS "update this group...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.