Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

While loop using Jquery does not display properly

jQuery loop jquery

  • Please log in to reply
1 reply to this topic

#1 Hasleen

Hasleen

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 21 November 2011 - 11:29 PM

i am using jquery and it is being called inside a while loop to display the output. the output for the first item is correct but for the second item it is not working properly. here is the code.....

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
         <script src="jquery-1.6.4.min.js" type="text/javascript"></script>
     <script type="text/javascript">
     $(document).ready(
     function()
     {
         $("#x").show();
       $("#y").hide();
        //$("$t:eq(1)").hide();
        //$("$t:eq(2)").hide();
         $("#x").click(

             function()
             {
               $("#y").show();
                // $("#t:eq(1)").show();
                 //$("#t:eq(2)").show();
             }
         );
     }
   );
  </script>
    </head>
    <body  bgcolor="#F3F3F3">
        <%
String userid=null;
if(session.getAttribute("visitor")!=null)
{
    userid=(String)session.getAttribute("visitor");
}
else
    {
response.sendRedirect("signin.jsp");
}
        if(request.getParameter("btnlogout")!=null)
    {
if(session.getAttribute("visitor")!=null)
    {
session.setAttribute("visitor",null);
response.sendRedirect("home.jsp");
}
else
    {
response.sendRedirect("pat_welcome.jsp");
}
}
%>
 <div> <%@include file="master.jsp"%></div>

        <div id="body1">
            <br/><br/><br/>
            <div style="margin-left:650px; ">
            <form action="#" method="get"><input type="submit" value="Logout" name="btnlogout" /></form></div>
               <br/><br/> <div id="main">
                    <div> <%@include file="leftdiv.jsp" %></div>
                    <div id="center">
                         <h2 style="color: indianred;">Articles</h2><br/>
<%
String id=null;
String dname=null;
String atitle=null;
String ades=null;
Connection con=null;
Statement smt=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:hcsds");
smt=con.createStatement();
rs=smt.executeQuery("select * from article");
while(rs.next())
{
id=rs.getString(1);
atitle=rs.getString(2);
ades=rs.getString(3);
dname=rs.getString(4);
%>

<div id="x" style="height:50px;width: 300px;">
<table border="0" cellpadding="5" cellspacing="10">
    <tr>
        <td>Title:</td>
        <td><%=atitle%></td>
    </tr>
    <tr>
        <td>Posted by:</td>
        <td><%=dname%></td>
    </tr>
</table>
</div>



<div id="y" style="height:100px; width: 300px;">
<table border="0" cellpadding="5" cellspacing="10">
    <tr>
        <td>Description:</td>
        <td><%=ades%></td>
    </tr>
</table>
</div>

       


<%
}
}
catch(Exception ex)
{
out.println(ex.getMessage());
}
%>

                    </div>
                    <div id="gap"></div></div></div>
        <center><hr size="2px" width="900px" color="gray"/></center>
        <div><%@include file="footer.jsp"%></div>
    </body>
</html>

Edited by Roger, 22 November 2011 - 07:25 AM.
added code tags, updated title, moved to correct forum

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 29 November 2011 - 05:06 AM

Sorry for the late response.
I don't see how the JQuery is called from inside the while loop. The jquery I see will use the tags generated in the while loop tho.

Try to avoid using static IDs for tags inside loops. IDs in html are meant to be unique, and I assume that may be a reason why JQuery only does 1. As it doesn't expect more than 1.
You can use the 'class' attribtue, which is not required to be unique.
  • 0





Also tagged with one or more of these keywords: jQuery, loop, jquery

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download