File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes set checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "set checkbox "checked" when a value in an selectbox changes" Watch "set checkbox "checked" when a value in an selectbox changes" New topic
Author

set checkbox "checked" when a value in an selectbox changes

mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
I have a Form with more rows, which all have the same layout (kind of an Array).

when a value is set in the selectbox, I want to change the checkbox on the same row to "checked".

the checkbox on every row has a unique value, and is defined like the following definition:
<input type="checkbox" name="addspelers" value="row.<%= (i += 1) %>.check"/>


the selectbox is defined like the following definition:
<select name="row.<%= (i += 1) %>.persid" size="1"/>

Now the onchange statement must be added............???
something like.....
<select name="row.<%= (i += 1) %>.persid" onchange="addspelers.checked" />
<option> bla bla blaaaa
</option>

I'm looking for the exact syntax for addspelers.checked , I want to change only the checkbox for the specific row....

Regards,
Mark
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Please show what you have tried so far and please UseCodeTags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
Below the code...
I try to set the value of the hidden field "speler.${details.speler.spelernr}" (on row 19) with the onchange in the other fields (row 29 f.e.), but it doesn't change....
The reason I do this, is that the servlet can check if a row is changed or not.
(Earlier I mentioned that I want to change the value of the checkbox, but that isn't necessary anymore.)

regards,
Mark

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

OK, now please isolate the code you are asking about. It'd be best to post an SSCCE, and make sure that there's no server-side markup in the HTML structure that you are trying to traverse. The browsers gets generated HTML, not JSP markup, so that's not relevant.

Essentially, you want to locate the element that receives the event, and backtrack up the DOM to find the <tr> element parent, and then find the appropriate child of that parent. This is almost trivial with jQuery, but a bit of work in raw JavaScript.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Since this is a problem with the browser not behaving the way you expect, looking at the server-side code will make troubleshooting this problem harder than it needs to be.

My suggestion would be to show us the source that the browser is displaying / interpreting. In your browser, select "show source" and copy/paste that into here.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
below the code when I use show source in the webbrowser



the "onchange" (on row 13) code looks fine to me, but the value shows me spaces when I display it in the servlet. (?)

and here is the Servletcode that displays the value:
the values from "kaart, gespeeld, doelpunten" are correct, the way I expect them.

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Your onchange method is referring to speler.10 as though this is something that the browser should naturally know about. Unfortunately browsers only know about standard objects from the DOM.

Rewriting your end-result code to use standard elements (and making it more of an SSCCE):

You can see that I added an ID for the hidden field in line 4 - this makes it easy for finding the object in the DOM tree. I then use that ID in the onchange handler in line 7 to get an object that JavaScript knows how to manipulate (which I can then do by setting the value).

By the way - if you use FireFox, you might want to get the WebDeveloper plugin, which gives you a nice error window for your JavaScript (and CSS) errors.
mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
Muchos gracias, It works fine!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: set checkbox "checked" when a value in an selectbox changes