SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
-
Apr 13, 2007, 03:45 #1
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Back to javascript gurus, problem is check box arrays
Hi
I'm having an array dynamically fetched from the database using php, in result, I'm getting a check box, with their corresponding value.
I wanted to make a javascript function so that if I uncheck a checkbox, corresponding value disappears.
Before going to the looping, I was trying to do a simple check - like this -
Code:function checkOtherValues() { if (document.myform.checkmember[0].checked==false) { document.myform.checkvalue[0].value=""; } }
Code:document.myform.checkmember.0 is null or not an object
Code:<input type="checkbox" name="checkmember[]" value="<?=$chmember[$i]?>" onClick="return checkOtherValues()"> <input type="text" name="checkvalue[]" size="5" value="<?=$chvalue[$i]?>" />
-
Apr 13, 2007, 04:02 #2
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think you need to show more code. Why do you set checkboxs value to an empty string? Not checked checkboxes are not submitted.
-
Apr 13, 2007, 04:08 #3
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks. basically I'm having two different arrays ... I'm trying to set the value of the second array to an empty string if I uncheck the corresponding first array.
-
Apr 13, 2007, 04:11 #4
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So, lets recap. You got 2 arrays. And these 2 arrays are somehow connected by having the same data or relative data? The second array is "mapped" to checkboxes?
-
Apr 13, 2007, 04:13 #5
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Got the problem. it was like this as I was using [] after the array name in my form. I added an id along with that and then in the javascript function fetched the value using getelementbyid method, and it's fixed now. I need to get the array things working now (since I don't have the number of returned rows)
-
Apr 13, 2007, 04:14 #6
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 13, 2007, 04:15 #7
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So problem is solved. Good.
-
Apr 13, 2007, 04:17 #8
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yeah i'll have to figure out a way now to get the array working. For the moment, as it is done now, it is only working for the first element of array.
Means, if I uncheck the first checkbox, the corresponding textfield value is going. But this is not the case for other checkboxes.
-
Apr 13, 2007, 04:24 #9
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
if in the form I call the function like
Code:onClick="return checkOtherValues(checkmember1, checkvalue1)"
Code:function checkOtherValues(a,b) { if (document.getElementById("a").checked==false) { document.getElementById("b").value=""; } }
It's giving me an "object required" error ...
-
Apr 13, 2007, 04:28 #10
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, but I dont see why you would like to empty checkbox's b value.
-
Apr 13, 2007, 04:29 #11
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no, I'm not emptying the checkbox's value, I'm emptying the corresponding array's value.
-
Apr 13, 2007, 04:31 #12
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You are using getElementById. That will return you a HTML node. I don't see anything in your code showing an Array.
-
Apr 13, 2007, 04:32 #13
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, I thought to go this way, since I can name each id of the array seperately, like id1, id2, etc.
-
Apr 13, 2007, 04:34 #14
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hm, I don't get you. Are you simply referering to checkboxes as an Array? Because, again, I don't see any Arrays in your code.
-
Apr 13, 2007, 04:40 #15
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, let me explain, sorry for not being clear earlier. Here is an example code - (example because the real one is getting fetched from the database dynamically using php)
Code:<form action="mypage.php" name="myform" id="myform" method="post"> <input type="checkbox" name="checkmember[]" id="checkmember1" value="1" onClick="return checkOtherValues(checkmember1, checkvalue1)"> <input type="text" name="checkvalue" id="checkvalue1" size="5" value="value1" /> <input type="checkbox" name="checkmember[]" id="checkmember2" value="2" onClick="return checkOtherValues(checkmember2, checkvalue2)"> <input type="text" name="checkvalue" id="checkvalue2" size="5" value="value2" /> <input type="checkbox" name="checkmember[]" id="checkmember3" value="3" onClick="return checkOtherValues(checkmember3, checkvalue3)"> <input type="text" name="checkvalue" id="checkvalue3" size="5" value="value3" /> </form>
Code:function checkOtherValues(a,b) { if (document.getElementById("a").checked==false) { document.getElementById("b").value=""; } }
-
Apr 13, 2007, 04:51 #16
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It would have helped from the start if you posted the code right away... There are several issues with your code. Let's start with the HTML.
HTML attributes should be in lowercase. Also, why do you use return before the function call? You are not returning anything from the function.
Next thing is that you need to pass the ids as a String. So you need to put them around quotes
Code:<input type="checkbox" name="checkmember[]" id="checkmember1" value="1" onclick="checkOtherValues('checkmember1', 'checkvalue1');">
Code:function checkOtherValues(a,b) { if (document.getElementById(a).checked == false) { document.getElementById(b).value = ""; } }
-
Apr 13, 2007, 04:57 #17
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi
Thanks a lot. It's solved now.
Thanks again.
Best.
-
Apr 13, 2007, 05:00 #18
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you wanna simplify your code... you can pass a reference of the checkbox itself instead of its name:
Code:<input type="checkbox" name="checkmember[]" id="checkmember1" value="1" onclick="checkOtherValues(this, 'checkvalue1');">
Code:function checkOtherValues(a,b) { if (a.checked == false) { document.getElementById(b).value = ""; } }
-
Apr 13, 2007, 05:08 #19
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah, this is pretty cool, didn't know this could be done this way as well ... thanks again.
Bookmarks