File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes how do i use checkbox as an active/inactive button for a form? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how do i use checkbox as an active/inactive button for a form?" Watch "how do i use checkbox as an active/inactive button for a form?" New topic
Author

how do i use checkbox as an active/inactive button for a form?

pratik mehta
Greenhorn

Joined: Jan 28, 2009
Posts: 3
Hi,

i am trying to use a checkbox as a switch for a form . As in if i check the checkbox . i can edit in the form if i uncheck it , i cant edit it and cannot access it. Is there anyway to do it? i am new to this , so a detailed help will be appreciated . thank you.
And also i want help in this as well.. suppose this is how my jsp looks like..

[] name

myname [_____] add button view button


now if i press add .. i get an additional field to it . similar to the one above and it can iterate. Is it possible with struts ? or should i use javascript? if javascript then how do i add the tag content in the javascript function to make it work.

Sorry my code is a bit messed up. I am trying a lot to make it work


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="WEB-INF/tlds/struts-html" prefix="html"%>
<%@ taglib uri="WEB-INF/tlds/struts-logic" prefix="logic"%>
<%@ taglib uri="WEB-INF/tlds/struts-bean" prefix="bean"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="javax.naming.*"%>
<%@page import="javax.servlet.*"%>

<html>
<head>
<script type="text/javascript">



function addrow() {
var a = document.getElementById('myTable').insertRow(0);
var b = a.insertCell(0);
var c= a.insertCell(1);
var d= a.insertCell(2);
var e=a.insertCell(3);
b.innerHTML="My Name";
c.innerHTML="Text";
d.innerHTML="Add";
e.innerHTML="View Button";

}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Master Table</title>
</head>
<body>

<html:form action="/table" method="post" >
<table id="myTable" width="613" border="0" align="center" bgcolor="#CCCCCC">
<tr>
<th width="147" scope="col">

<div align="left">
<html:checkbox property="checked">SYSTEM
</html:checkbox>

</div>

</th>
<td width="218" scope="col"> </td>
<th width="234" scope="col"> </th>
</tr>
<tr>
<td> </td>
<c:set var="count" value="3"/>
<c:if test="${param.count>0}"><c:set var="count" value="${param.count}"/></c:if>
<c:forEach var="i" begin="1" end="${count}">
${i})
<td>My Name </td>
<td>
<html:text property="myName" />
<html:button property="add" value="Add" />
<html:button property="view" value="View" onclick="show_popup();" />
</td>
</c:forEach>
</tr>

</table>
</html:form>
</body>
</html>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9345
    
  19

The first thing that you are asking can be implemented by using javascript. Just give a unique ID to every text box and other input fields with a row number. Like if you have this structure

checkbox | name text box | age text box | button

And you have multiple rows with this type of arrangement, then you must have a naming convention for this. Like this must be the name for the fields in different rows

chk1 | name1 | age1 | button1
chk2 | name2 | age2 | button2

Now on the checking the check box, you can call a javascript function with a parameter like

<input type="checkbox" name="chk1" onclick="func(1)" />

now in the javascript you can have this type of code



I have not tried this code but it should work. Also for the other thing, I didn't understand the problem properly and neither am I a pro at struts. So I think you should ask it in the struts forum. I can give you a normal javascript solution if you want but you will have to provide some more details of the problem...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
pratik mehta
Greenhorn

Joined: Jan 28, 2009
Posts: 3
hey thanks for the reply. it worked fine.. alright the second problem is that

i have a text field and an add button so everytime i click the add button i get a new text field.
how do i do that implementing javascript or struts?

how do i define the function for it?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9345
    
  19

I don't know about struts, but in javascript it is easy. The code that you have written looks good. What's the problem that you are facing??
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how do i use checkbox as an active/inactive button for a form?