Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is my code

HTML
 <input type="hidden" id="cid" value="<%=cid %>">
 <input type="hidden" id="email" value="<%=user%> ">
<input type="hidden" id="gid" value="<%=grid %>">
<input type="hidden" id="postid" value="<%=postid %>">
<input type="text" value="<%=msg%>" name="comment" id="comment">
<input type="submit" value="submit" id="<%=cid %>" class="sub"/>


jquery function is


JavaScript
      $(.sub ).click(function(e) { 
    e.preventDefault();
     var cid=$("#cid").val();
     var comment = $("#comment").val();
     var email=$("#email").val();
     var gid=$("#gid").val();
     var postid=$("#postid").val();
     var value ='comment='+comment+'&cid='+cid+'&email='+email+'&gid='+gid+'&postid='+postid;
 
     $.ajax({
     url: "update.jsp",
     //type: "post",
     data: value,
     cache: false,
     success: function(data) {
    
     $("#comment_display").html(data).slideDown('fast');
   
     }
     });
});
}


when i click on the submit button the id is<%=cid%> is pass to the $(.sub ).click(function(e). how can i get id dynamically
Posted
Updated 26-Dec-16 19:27pm

You can get the ID attribute of the submit button clicked with a class sub like this:

JavaScript
$('.sub').click(function (e) {
    e.preventDefault();
    var cid = this.id;  // get the button id here
    alert(cid);         // you can also check it using alert

    // Your code goes here...
});


FIDDLE DEMO[^]
 
Share this answer
 
v2
Comments
User-10031173 10-Oct-13 3:07am    
thanks for ur its works perfectly
User-10031173 10-Oct-13 3:09am    
can we update the data without reloding the page using ajax.
i have used ajax but its not working where the problem please suggest me here is my code

<script type="text/javascript">
$(document).ready(function() {
$(".aaa input:image").click(function(e) {var id=$(this).attr('id');
e.preventDefault();
var cid=$("#"+id+"cid").val();
alert(cid);
var comment = $("#"+id+"c").val();
var email=$("#email").val();
var gid=$("#gid").val();
var postid=$("#postid").val();
var value ='comment='+comment+'&cid='+cid+'&email='+email+'&gid='+gid+'&postid='+postid;

$.ajax({
url: "update.jsp",
//type: "post",
data: value,
cache: false,
success: function(data) {
alert("ok");
$("#comment_display").html(data).slideDown('fast');

}
});
});
});
</script>

and my jsp page is

<%
System.out.println("comment information");

int cid1=Integer.parseInt(request.getParameter("cid"));
System.out.println("comment information cid is..............."+cid1);
int gid1=Integer.parseInt(request.getParameter("gid"));
int postid1=Integer.parseInt(request.getParameter("postid"));
String email=request.getParameter("email");
String comment=request.getParameter("comment");

try{
Connection con=(Connection) new DB2Connection().getDatabaseConnection();
System.out.println("connection test in update.jsp");
Statement st=con.createStatement();

PreparedStatement ps=con.prepareStatement("update user_comment set comment=? where cid=?");

System.out.println("after query in update.jsp");
ps.setString(1,comment);
ps.setInt(2,cid1);
int k= ps.executeUpdate();
if(k>0){
Statement st1=con.createStatement();
ResultSet rs1=st1.executeQuery("select * from user_comment where gid='"+ gid1 + "' and postid=" + postid1);

System.out.println(gid1+"comment information");

while (rs1.next()) {
String msg = rs1.getString("comment");
String user = rs1.getString("email");
int cid=rs1.getInt("cid");
int postid=rs1.getInt("postid");
int grid=rs1.getInt("gid");
%>
<input type="hidden" name="cid" value="<%=cid %>" id="cid<%=cid%>"/>
<input type="hidden" name="pid" value="<%=postid %>" id="pid<%=postid%>"/>
<input type="hidden" name="grid" value="<%=grid %>" id="grid<%=postid%>"/>
<input type="hidden" name="email" value='<%=session.getAttribute("email")%>' id="email" />
<div id="comment_display">
<p
style="border:0px solid skyblue;width:auto;height:auto;font-weight: bolder;" id="p"><%=user%>
:     <%=msg%>     

<img alt="delete" src="images/Edit.png" height=15 width=15 style="cursor:pointer;" id="edit" />
<img alt="delete" src="images/cancel-icon.png" height=15 width=15 style="cursor:pointer;" önclick="deletecomment('cid<%=cid%>','pid<%=postid%>','grid<%=postid %>','email','review_display<%=postid%>');">
</p>
</div>
<%
}
}
}
catch(Exception e){
}

%>
Palash Mondal_ 10-Oct-13 8:10am    
Hi Swapna, I am not sure about the jsp code, as I am not a Java developer and your jQuery code seems fine...
You can do something like this(I hope I am understanding your question correctly):
#1: Add a custom attribute like "mySubmit='cid'" to your submit button.
#2: Find that submit button by using jQuery selector, for ex: $("*[mySubmit='cid']")
#3: Then get the value of the 'id' attribute, for ex: $("*[mySubmit='cid']").attr('id')

or if you only want to fetch the submit button then omit step 3.
 
Share this answer
 
v3
kolklkkkkkkkkkkkolklkkkkkkkkkkkolklkkkkkkkkkkkolklkkkkkkkkkkkolklkkkkkkkkkk
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900