0
  out.println("<html>");
    out.println("<head>"
            + "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js\"></script>"
            + "<script>"
            + "$('input[type=\"checkbox\"]').on('change', function(){$('input[type=\"checkbox\"]').not(this).prop('checked', false);});"
            + "</script>"
            + "</head>");
    out.println("<body>");

I want to check only 1 checkbox at a time and would like to use this script to do that. The script itself is working, however in the servlet it isn't.

How can i add this script to the servlet?

2 Answers 2

1

It's unlikely a servlet problem. You execute your jQuery code before the page body is loaded, thus you have no checkboxes yet and your events are not registered. Try to wrap it into $(function() {...}).

out.println("<html>");
out.println("<head>\n"
        + "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js\"></script>\n"
        + "<script>\n"
        + "$(function() {\n"
        + "  $('input[type=\"checkbox\"]')\n"
        + "    .on('change', function(){$('input[type=\"checkbox\"]').not(this).prop('checked', false);});\n"
        + "});\n"
        + "</script>\n"
        + "</head>");
out.println("<body>");
Sign up to request clarification or add additional context in comments.

Comments

0

You can wrap up your JQuery code inside a function so that it will be executed once your document is ready not before that like this :

$( document ).ready(function() {
$('input[type=\"checkbox\"]').on('change', function(){$('input[type=\"checkbox\"]').not(this).prop('checked', false);});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.