Jump to content

copying with jquery


phpnoob

Recommended Posts

Yesterday i was made a simple jquery life search to mysql, and today i want to make a auto complete jquery code, i made it in half, and i'm stock, help me, i'm not good in js, but i was check everything in here but cant find a good code.the situation is this, it have 3 table "td" 1 name and 2 link, when i click the name td, the jquery start and copy those 3 table "td" all in 1 input value, i mean i have 3 input 1 for name and 2 for link. atm i have the simple code for put data to value, now i need the copy code, i find 1 code but that copy the html tag to, that not good :(

<script type="text/javascript">$('#TD').click(function(){  $('#input').attr('value', '1');});

Help me, thx

Link to comment
Share on other sites

It would help if we had the html table code as well, to see what we are dealing with if you have <td id="TD"> whatever values</td> you would use simply use

$('#TD').click(function(){var current_value = $(this).html();  $('#input').attr('value', current_value);});

however if you have <td id="TD"><span> whatever values</span></td> you will have to target the first parent element of the text ie span, not td

$('#TD').click(function(){var current_value = $(this)children("span").html();  $('#input').attr('value', current_value);});

OR

$('#TD span').click(function(){var current_value = $(this).html();  $('#input').attr('value', current_value);});

Link to comment
Share on other sites

sry for forgot to write the full html code, here it is

<html><head><script type="text/javascript" src="jquery.js"></script></head><body>Name:<input id="input" value=""/> | Link1:<Input id="link1" value=""/> | Link2<input id="link2" value="" /><table><tr><td id="TD">Name</td><td>Link1</td><td>Link2</td></table> <script type="text/javascript">$('#TD').click(function(){  $('#input').attr('value', '1');});</script></body></html>

and want to copy the name to input id and link1 to link1 value and link2 to link2 value, by clicking only to name td

Link to comment
Share on other sites

thx it work, but i find something bad, why not work the code if it have number in copy id? :(good code, but if the td id have number and i write it in here "var current_value = $('#linka').html();" then its stop working

<html><head><script type="text/javascript" src="jquery.js"></script></head><body>Name:<input id="input" value=""/> | Link1:<Input id="link1" value=""/> | Link2<input id="link2" value="" /><table><tr><td id="TD"> Game Name1 </td><td id="linka" style="display:none;"> Game1 link1 </td><td id="linkaa" style="display:none;"> Game1 link2 </td></tr><script type="text/javascript">$('#TD').click(function(){var current_value = $(this).html();  $('#input').attr('value', current_value);});$('#TD').click(function(){var current_value = $('#linka').html();  $('#link1').attr('value', current_value);});$('#TD').click(function(){var current_value = $('#linkaa').html();  $('#link2').attr('value', current_value);});</script></table> <table><tr><td id="TDb"> Game Name2 </td><td id="linkb" style="display:none;"> Game2 Link1 </td><td id="linkbb" style="display:none;"> Game2 link2 </td></tr><script type="text/javascript">$('#TDb').click(function(){var current_value = $(this).html();  $('#input').attr('value', current_value);});$('#TDb').click(function(){var current_value = $('#linkb').html();  $('#link1').attr('value', current_value);});$('#TDb').click(function(){var current_value = $('#linkbb').html();  $('#link2').attr('value', current_value);});</script></table></body></html>

i made a little exaple, you see 2 tr and i have to add TDb and linkb and linkbb in id, cause the code stop if it have number in id, i hope some1 can help me

Link to comment
Share on other sites

all with one click

$(document).ready(function() 	{ $('#TDb').click(function(){var current_value = $(this).html();  $('#input0').attr('value', current_value);  current_value = $(this).parent("tr").children("td").eq(1).html();  $('#link1').attr('value', current_value);	current_value = $(this).parent("tr").children("td").eq(2).html();  $('#link2').attr('value', current_value); }); });

Link to comment
Share on other sites

all with one click
$(document).ready(function()  	{ $('#TDb').click(function(){var current_value = $(this).html();  $('#input0').attr('value', current_value);  current_value = $(this).parent("tr").children("td").eq(1).html();  $('#link1').attr('value', current_value);	current_value = $(this).parent("tr").children("td").eq(2).html();  $('#link2').attr('value', current_value); }); });

thx it work and maybe its better then mine, cause i don't need id in 2+3 td only in the first td if not prob i change this codecurrent_value = $(this).parent("tr").children("td").eq(2).html(); to this current_value = $(this).parent('tr').children('td').eq(2).html(); i don't know why but in php it make error even if i modifiyng itcurrent_value = $(this).parent(/"tr/").children(/"td/").eq(2).html();
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...