简体   繁体   中英

Get text from td and print it in a input

I've a table and I need to get the text of the td I've clicked and print it in a input box. Now I've this code but it prints the last value of the table.

$("td").click(function(){  
    $("td").each(function(index){
        $("#prueba").val(($(this).text())); 
    }); 
}); 

You can remove the $("td").each(function(index){

So, this would be enough-

$("td").click(function(){  

    $("#prueba").val(($(this).text())); 
}); 

with for each loop you are assigning the text of each Td block to element with id prueba , at the end it has value of the last cell(td)

$("td").click(function(){  
    //$("td").each(function(index){ // not needed
        $("#prueba").val(($(this).text())); 
    //}); // not needed
}); 

just remove the each, you don't need it!

$("td").click(function(){  
    $("#prueba").val(($(this).text()));
}); 

you can see here the solution: http://jsfiddle.net/GNkxm/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM