-2

I am having checkboxes in a table.Now to check which of the checkboxes are checked i wrote following code and i concatenate the values of these checked checkboxes seperated by a comma.But the code returns null value.

function myfun() {        
    var s="";
    var flags = document.querySelectorAll('.select_all_mail');//Here this is class of checkboxes
    for (var i=0, iLen=flags.length; i<iLen; i++) {
        if (flags[i].checked) {
            //here also its printing all the values of check boxes weather they are checked or not
            console.out(flags[i].value);
            s=s+flags[i].value+',';
        }
     }
     s=s.substring(0,s.length-1);

    //I want this s to pass to a servlet so i do following code
     document.myinbox.hiddenValue.value = s;  //here myinbox is name of form  
     document.myinbox.submit(); 
}

Is their something wrong with code?Please help

user3499007
  • 5
  • 1
  • 6
  • 3
    Your function has no return statement. And `System.out.println()` is JavaScript? Is that part of a custom API? – cookie monster Apr 04 '14 at 16:47
  • 1
    By the way, `System.out.println` isn't really javascript, it's java. The js equivalent is `console.log()`. And yes, you're missing a return statement. – morten.c Apr 04 '14 at 16:48
  • Also, it should be `s.length` not `s.length()`. – cookie monster Apr 04 '14 at 16:50
  • @morten.c i edited according to requirement.Please check – user3499007 Apr 04 '14 at 16:51
  • Why did you change the code in the question? Please show the *real code* from the start. – cookie monster Apr 04 '14 at 16:51
  • 2
    It seems like the OP is very confused between Java and Javascript. – Barmar Apr 04 '14 at 16:51
  • @cookiemonster That was another javalander – morten.c Apr 04 '14 at 16:51
  • 1
    @user3499007 Someone edited your post to fix up the indentation. Why did you undo it when you added the additional code? Please post properly-indented code, to make it easier for people to help you. – Barmar Apr 04 '14 at 16:52
  • If your code isn't working, the first thing you should do is check for errors in the Javascript console. You should be getting error for `System.out.println` and `s.length()`, and they would have pointed the way to the solution without having to post. – Barmar Apr 04 '14 at 16:54
  • @Barmar I edited my code but still its unable to detect which checkboxes are checked or not.Also on other servlet the value of this string s when printed gives null. – user3499007 Apr 04 '14 at 17:00
  • `console.out` should be `console.log`. See http://jsfiddle.net/PSkVp/2/ – Barmar Apr 04 '14 at 17:07
  • @Barmar Then is their something wrong with the way am moving to the servlet?Am doing like this :
    here deletemail is my servlet
    – user3499007 Apr 04 '14 at 17:18
  • The function is submitting a form, so it goes to the `action` attribute of the form, not the `href`. – Barmar Apr 04 '14 at 17:22
  • @Barmar What i wanna do is just on click of this button i wanna move the string s to next servlet.How to do it?Please help.I dont want it to be action of whole form – user3499007 Apr 04 '14 at 17:27
  • That seems to be a completely different question than the one you posted. Sounds like you need to use AJAX. – Barmar Apr 04 '14 at 17:32

1 Answers1

0

I think you are missing the return statement.
Add the following code:

return s;

Alternatively you could declare "s" outside of the function so it is available globally, but this is not good coding practice.

EDIT

Now that the code sample has been updated slightly in the original post, the return appears not to be needed as the value is passed in the form submit. The problem is likely due to the System.out.println and possibly some other syntax errors in the JavaScript.

RacerNerd
  • 1,579
  • 1
  • 13
  • 31
  • Please don't suggest return-by-side-effect. It's an idea that sounds pretty good to beginners, but is in fact incredibly bad. – 15ee8f99-57ff-4f92-890c-b56153 Apr 04 '14 at 16:52
  • @RacerNerd But why its not checking that checkboxes are checked or not.?It is printing values of all checkboxes – user3499007 Apr 04 '14 at 16:55
  • JQuery is probably easier to access all the checked elements with a single select. Since you will have only selected elements, then you can loop through and print everything you have. Check out this, http://stackoverflow.com/questions/11292778/use-jquery-to-get-values-of-selected-checkboxes. – RacerNerd Apr 04 '14 at 16:59
  • @user3499007: *"...the code returns null value"* - *"...It is printing values of all checkboxes"*. ***Which is it?*** – cookie monster Apr 04 '14 at 17:00
  • jQuery isn't needed for this simple problem. what's needed is some better information in the question. – cookie monster Apr 04 '14 at 17:00
  • @cookiemonster what are you asking?"Which is it".?You want me to post html code part ? – user3499007 Apr 04 '14 at 17:02
  • @user3499007: I'm saying that on one hand, you say you're getting a null value, and on the other hand you're saying it prints all values. You need to put together a full example that clearly demonstrates the issue you're experiencing. – cookie monster Apr 04 '14 at 17:03
  • @cookiemonster I am getting right values when i print values in the javascript using console.out but when i print it on my next servlet which i go to by
    here deletemail is my servlet.i get null value on that servlet
    – user3499007 Apr 04 '14 at 17:22