Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
code
JavaScript
 while(rs.next())
                        {
                         String friendname = rs.getString(2);
                         
                         out.print("<table><tbody><tr><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("<button type=\"button\" onclick='javascript:return friendnamebtn(this)' id=\"btn_friends\">");
                         out.print("<button type=\"button\" onclick='javascript:return friendnamebtn("+ friendname +")' id=\"btn_friends\">");
                         out.print("Add" + friendname);
                         out.print("</button>");
                         out.print("</td></tr></tbody></table>");
               
                     }
                     out.print(""); 

javascript code

function friendnamebtn(friendname){
 //   var friends = document.getElementById("btn_friends");
    alert(friendname+"add");
}
Posted
Updated 15-Oct-15 22:41pm
v2
Comments
Andy Lanng 16-Oct-15 4:41am    
Question?
Member 9671810 16-Oct-15 4:47am    
getting error "Uncaught ReferenceError: daisy is not defined" when i debug.

1 solution

You need to put quotes around friendname in the onclick so it is also correct javascript. If you look at the resulting html/javascript mix you'll notice the error. This is why creating websites by pasting html, javascript and values together in hope of something that works is just looking for trouble.

By the way, it is pretty strange to add your script element in a td element. It is also strange you use rs.getString(2) after you put that value in friendname. This indicates some sloppy coding. But what's really to worry about is just putting whatever rs.getString(2) holds directly in your response without html encoding it first.

Good luck!
 
Share this answer
 
Comments
Member 9671810 16-Oct-15 5:13am    
sorry i was just checking that, there might be error while defining friendname i tried this friendnamebtn("+ friendname +") but it is not working.
E.F. Nijboer 16-Oct-15 6:07am    
But you need to surround it with another set of quotes for javascript. The ones you have now are for your java code.
Member 9671810 16-Oct-15 8:05am    
thanks. :) it worked friendname(\"" + friendname + "\") :)

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