Skip to content →

Checking checkboxes with jQuery and Cognos

Want to know how to check some checkboxes by default in COGNOS? This could help your prompt page.. Get your report setup with jQuery by following this post.

On your prompt page or report, create a prompt with a checkbox. Surround it with two HTML Items, and put the following in the first:

[code]<div id="checkbox>[/code]

and the one after it should be:

[code]</div>[/code]

Place another HTML item to the right of the report title at the very top. Put your basic code in there, it should resemble something like this:

[code]<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js</a>"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function() {

});
</script>[/code]

Once I had this in place, I used the following JS to select the checkbox:

[code]jQuery("#checkbox input[type=checkbox]:eq(1)").attr(‘checked’,’checked’);[/code]

Let’s take a look at that particular call:

“#checkbox input[type=checkbox]:eq(1)”

This takes the checkbox div we created, and finds every input with a type attribute of checkbox. This actually returns 5 objects in my test report – an empty/hidden input, and 4 static choices I created. To select the first one, I just point to the [1] position using :eq(1). I could use 2 for the second, 3 for the third, so on.

.attr(‘checked’,’checked’);

This is jQuery’s way of saying “check this box!” All of that in one line. If you need clarification, please let me know.

(Full code)

[code]<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery("#checkbox input[type=checkbox]:eq(1)").attr(‘checked’,’checked’);
});
</script>[/code]

Published in Uncategorized

4 Comments

  1. This is a great article. I am new to jQuery and found that this will save me a lot of time. I was wondering how would you select multiple items in a checkbox. I have an array of items that should be selected in the checkbox.

    • Bertinneau Bertinneau

      Thanks for the kind words Matthew, glad it could help. If I needed to select multiple items in a checkbox, I’d just modify the selector.

      jQuery("#checkbox input[type=checkbox]:eq(1)")

      The :eq(1) part of this selects a single checkbox. If you remove that, you can select all of the items. You’d have to get creative to pick and choose which checkboxes you’d like to check, and I don’t work with Cognos anymore so I can’t really be of help there (unfortunately). Let me know if you have any other questions.

  2. Dev Dev

    Hi Nic,

    I am also not good at JS/JQuery but It seems that your post is the one I was searching for.

    My report has a filter which is a Multi-Select Check Box Value Prompt, Vintage Year. It has values as 2010,2011 and 2012. Now, I want to set its default value as 2011 i.e. when the report runs, it should be pre-filtered on 2011.

    Apart from the 3 HTML items mentioned above, what else should be done to fulfill my requirement?

    Thanks in advance.

    Best Regards
    Dev

  3. Dev Dev

    Hi Nic,

    I got it to work without any script.

    Thanks anyways.

    Best Regards
    Dev

Leave a Reply

Your email address will not be published. Required fields are marked *