views:

1020

answers:

3

Hey guys,

I have a table setup like 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>

I basically want the anchor to change to a text box containing the href when the link is click, below are example results:

<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 is click or the text box is unfocused, any text boxes go back to being a anchor and the clicked one changes to a text box.

Cheers

Eef

+1  A: 
$(".url").click(function()
{
var link = $(this).find("a");
link.hide();
$(this).append("<input id'txtUrl' value" + link.attr("href") + " />");
});
Aliixx
+4  A: 

This is a start. Add a click event to the links and a blur event to the inputs using live().

$(function() {
  $("td.url a").live("click", function() {
    var parent = $(this).parent();
    $(this).replaceWith("<input type='text' value='" + $(this).attr("href") + "'");
    parent.children(":text").focus();
    return false;
  });
  $("td.url :text").live("blur", function() {
    $(this).replaceWith("<a href='" + $(this).val() + "'>");
  });
});

That being said, for this kind of thing I prefer not to delete elements from the DOM like this. Instead I prefer to just hide/show elements as appropriate. For example:

<table>
  <tr>
    <td class="url">
       <a href="http://www.domainname.com/page1.html" />
       <input type="text">
    </td>
  </tr>
</table>

with:

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");
  });
});
cletus
this is a more graceful solution
Aliixx
Won't it be necessary to focus the input field when it's clicked? So that it will force the blur method to be called if something else is clicked...
peirix
A: 

Something like:

$("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);
    });
});

totally untested, of course

Victor