Randomblue
Randomblue

Reputation: 116403

jQuery replaceAll() function

I want to replace every link by an input box whose value is the URL of the link. I would like to use the jQuery replaceAll() function. The standard format is

$(content).replaceAll(target);

My question is: how do you refer to the target object? In other words, in the following code, what should I replace TheCurrentLink with?

$('<input>').attr('value', TheCurrentLink.href}).replaceAll($("a"));

Upvotes: 0

Views: 631

Answers (3)

Alfonso Rubalcava
Alfonso Rubalcava

Reputation: 2247

Try:

$("a").replaceWith(function() {
   return "<input type='text' value='"+$(this).attr("href")+"' />";
});

Test here.

Upvotes: 2

SeanCannon
SeanCannon

Reputation: 78006

I'd use replaceWith() instead:

    $('a').each(function(){
       $(this).replaceWith('<input type="text" value="' + $(this).attr('href') +  '" />');
    });

Working example: http://jsfiddle.net/AlienWebguy/6KP4H/

Working Example 2, added logic to convert inputs back into links: http://jsfiddle.net/AlienWebguy/6KP4H/1/

Upvotes: 1

Paul
Paul

Reputation: 141917

You need to do something more like this:

$('a').each(function(){
    var $a = $(this);
    $a.replaceWith($('<input/>').attr('value', $a.attr('href')));
});

Upvotes: 3

Related Questions