Friday, February 11, 2011

Ajax Check Box Check Uncheck Example

ajaxSample.jsp :
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<html>
<head>
<script type="text/javascript">
function showHint()
{
var xmlhttp;
var str = document.getElementById("checkBox").checked;
var temp;
if(str){
    temp = "Checked"
}else{
    temp = "Un Checked";
}

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajaxResponse.jsp?param="+temp,true);
xmlhttp.send();
}
</script>
</head>
<body onload="showHint()">
<h1>Ajax Check Box Check Un Check Example</h1>
<input type="checkBox" id="checkBox" name="checkBox" value="checked" onclick="showHint()"/> <span id="txtHint"></span>

</body>
</html>


ajaxResponse.jsp :

<%out.println(request.getParameter("param"));%>

No comments: