ankur jadiya
ankur jadiya

Reputation: 137

how to get the value of button into other jsp using java script

i have to get the button value on other page of jsp and execute <div> tag based upon condition my index.jsp code is this

 <%@ 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 prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<html>
<head>
<script type="text/javascript">
   function popuponclick()
   {
      my_window = window.open("file.jsp",
       "mywindow","status=1,width=350,height=150");

 }



</script>
    </head>
    <table>
    <tr>
    <td><button type="button" onclick="popuponclick()" value="1" name="name">GetAllEmployeeByID</button></td>
    </tr>
    <tr>
    <td><button type="button" onclick="popuponclick()" value="2" name="name">GetEmployeeByName</button></td>
    </tr>
     </table>
    </body>
    </html>

and my second.jsp is this

  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml"%>
<%@page import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script>
    function myfuntion(String name)
    { 
        location.
        document.getElementById("div1");
        }

</script>

</head>
<body>
    <form action="/EmployeeWeb/GetEmployee">
        <div id="div1" style="display: block;">
                <table>
                    <tr>
                        <td width="29">Find_Employee_By_Name:</td>
                        <td><input type="text" name="findbyname" /></td>


</tr>
                    <tr>
                        <td><input type="submit" name="submit" value="find"></td>
                    </tr>
                </table>
            </div>

 <div id="div2" style="display: none;">
                <table>
                    <tr>
                        <td width="29">Find_Employee_By_Name:</td>
                        <td><input type="text" name="findbyname" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" name="submit" value="find"></td>
                    </tr>
                </table>
            </div> 
    </form>
     <button onclick="myfunction()"></button>
</body>
</html>

how i will get the button value insecond jsp so that based upon condition i can run that <div> tag part

Upvotes: 1

Views: 1657

Answers (2)

Michael Malinovskij
Michael Malinovskij

Reputation: 1422

If I got your question right, try something like this. You'll need to add some IDs to your buttons to select them easily.

replace (with slashes) \button id here\ with button ID, and \some value\ to the value you want to match with (for example - 2).

if (document.getElementById('\button id here\').value == \some value){

} else {

}

and btw, in second jsp you got this construction: location.document.getElementById

There is no "document" property in "location" objects, use it without "location".

Upvotes: 0

Elton Schivei Costa
Elton Schivei Costa

Reputation: 109

Try this in the index.jsp page:

function popuponclick()
{
    var my_window = window.open("file.jsp", "mywindow","status=1,width=350,height=150");

    // Gets the two divs
    var div1 = my_window.document.getElementById('div1');
    var div2 = my_window.document.getElementById('div2');

    // Hides the two divs
    div1.style.display = 'none';
    div2.style.display = 'none';

    // Check what the value of the button pressed and displays the correct div
    if(this.value = '1')
        div1.style.display = 'block';
    else
        div2.style.display = 'block';
}

Upvotes: 2

Related Questions