3

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.

3 Answers 3

13

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") + "'>"); //closing angle bracket added
    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");
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

this is a more graceful solution
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...
add the closing bracket in code ( $(this).replaceWith("<input type='text' value='" + $(this).attr("href") + "'>"); //closing angle bracket added) line 4
FYI: The jQuery live() function is deprecated. Use the on() function instead.
2
$(".url").click(function()
{
var link = $(this).find("a");
link.hide();
$(this).append("<input id'txtUrl' value" + link.attr("href") + " />");
});

1 Comment

Doesn't It append that input to all the ".url"?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.