JQuery change input text
I have a table as shown below:
<table>
<tr>
<td class="url">
<a href="http://www.domainname.com/page1.html" />
</td>
</tr>
<tr>
<td class="url">
<a href="http://www.domainname.com/page2.html" />
</td>
</tr>
<tr>
<td class="url">
<a href="http://www.domainname.com/page3.html" />
</td>
</tr>
</table>
Basically, I want the binding to change into a text field containing href when the link is clicked, the following are examples:
<table>
<tr>
<td class="url">
<input type="text" value="http://www.domainname.com/page1.html" />
</td>
</tr>
<tr>
<td class="url">
<a href="http://www.domainname.com/page2.html" />
</td>
</tr>
<tr>
<td class="url">
<a href="http://www.domainname.com/page3.html" />
</td>
</tr>
</table>
When another anchor tag clicks or the text field is not focused, all text fields return to the anchor, and the clicked field returns to the text field.
+3
3 answers
This is the beginning. Add a click event to the links and an input blur event with live().
$(function() {
$("td.url a").live("click", function() {
var parent = $(this).parent();
$(this).replaceWith("<input type='text' value='" + $(this).attr("href") + "'>"); //closing angle bracket added
parent.children(":text").focus();
return false;
});
$("td.url :text").live("blur", function() {
$(this).replaceWith("<a href='" + $(this).val() + "'>");
});
});
As the saying goes, for this kind of thing, I prefer not to remove elements from the DOM like this. Instead, I prefer to simply hide / show items as needed. For example:
<table>
<tr>
<td class="url">
<a href="http://www.domainname.com/page1.html" />
<input type="text">
</td>
</tr>
</table>
from:
td.url input { display: none; }
td.edit a { display: none; }
td.edit input { display: block; }
and
$(function() {
$("td.url a").click(function() {
var a = $(this);
a.next().val(a.attr("href")).focus();
a.parent().addClass("edit");
return false;
});
$("td.url :text").blur(function() {
var txt = $(this);
txt.prev().attr("href", txt.val());
txt.parent().removeClass("edit");
});
});
+13
- :
$("td.url a").bind("click", function(){
var clickevent = arguments.callee;
$("td.url input").trigger("blur");
$(this).replaceWith("<input type='text' value='"+$(this).attr("href")+"'/>");
$("td.url input").bind("blur", function(){
$(this).replaceWith("<a href='"+$(this).val()+"' />");
$("td.url a").bind("click", clickevent);
});
});
,
0