Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have made dynamic buttons, using this code.
Java
out.print("<table>");
 out.print("<center><h1>Result:</h1></center>");
 rs=ps.executeQuery();                
 /* Printing column names */
 ResultSetMetaData rsmd=rs.getMetaData();
 out.print("<tr>");

 while(rs.next())
    {
     out.print("<td>"+rs.getString(2));              
     out.print("  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>");
     out.print(" <script src=\"js/index.js\"></script>");
     out.print("<button type=\"button\" onclick=friendnamebtn() id=\"btn_friends\">");
     out.print("Add" + rs.getString(2));
     out.print("</button>");
     out.print("</td></tr>");
 }
 out.print("</table>");    

my javascript which is stored in index.js
JavaScript
function friendnamebtn()
{
     var friends = document.getElementById("btn_friends");
     alert(freinds+"add");
}

Actually when i click on that button, i want that id to be fetched and add that persons to the logged in person's friend list. I called the buttons event using javascript how do i connect those two people.
Posted
Updated 15-Oct-15 20:44pm
v2

1 solution

It may be because you are missing the Quote while declaring the javascript event. Can you try something like this?

Java
out.print("<button type=\"button\" onclick='javascript:return friendnamebtn(this)' id=\"btn_friends\">");


JavaScript
function friendnamebtn(sender)
{
     //write your code here
     return false;
}
 
Share this answer
 
Comments
Member 9671810 16-Oct-15 3:07am    
it's not working.. :/ why is this sender? and i have javascript code in separate file named index.js.
_Amy 16-Oct-15 3:12am    
Then you should check your js file path. I think js path is not referenced properly. Try to hit F12 in chrome browser and go to console and check the error.

See: Debugging JavaScript[^].
Member 9671810 16-Oct-15 3:26am    
thanks :) it's working :) now can you please suggest me if i want to connect those two people,(i mean i need to write database connectivity code. so can i write in javascript or do i need to use some other logic :))
_Amy 16-Oct-15 3:28am    
Sure I can help. You can start a new thread related to your problem. And please state that what have you tried so far?
Member 9671810 16-Oct-15 3:30am    
and hey, if i want to pass argument as onclick=friendnamebtn(rs.getString(2)) so that i can get there id or name. then???

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