1

Can someone please tell me how to set a checkbox dynamically in Javascript?

function displayResults(html){
    var v = html.split(",");
    document.getElementById('fsystemName').value = v[0];
    if (v[1] == "true"){
        document.getElementById('fUpdateActiveFlag').checked = true;
    }
}

So I am passing some values from my controller separated by commas. For the checkbox, when the value is true I want it to tick the box.

EDIT: When a value is changed from a dropdown box it calls this displayResults method as its return statement:

$('#uINewsSystemList').change(function() {
    $.get("/live-application/SystemById", {systemid: $("#systemList").val()}, displayResults, "html");

I want it to update some other values such as textboxes and checkboxes. I can get the fsystemName to populate with the appropriate value but the fUpdateActiveFlag always stays unchecked.

maloney
  • 1,633
  • 3
  • 26
  • 49

1 Answers1

0

In your question, you posted a javascript example, not JSP.

In JSP you can use a for loop to write the checkbox like this:

<% for (Element element : elementList) { %>
<input type="checkbox" name="<%=element.getName() %>" value="<%=element.getValue() %>" <%=element.getChecked() ? "checked" : "" %> />
<% } %>

Will result in:

<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
greuze
  • 4,250
  • 5
  • 43
  • 62
  • Apologies, I meant Javascript – maloney Mar 06 '13 at 16:49
  • If you are using jQuery, take a look to this: http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript Otherwise, document.getElementById('fUpdateActiveFlag').checked = true; should do the work – greuze Mar 06 '13 at 17:01