0

I have a div with some text <div>This is my initial text</div> And I'd like to replace the text 'initial' with an input box which I already have created

Something like:

input=$("#myinput");
$("div").find("initial").replaceWith(input);

OR:

input=$("#myinput");
$("div").html().replace('initial',input);

But neither of these work

Any thoughts?

4
  • var inputString = document.createElement('div').appendChild(input[0]).innerHTML;. With the input as a string you can do a normal replace. Commented Jun 3, 2013 at 17:41
  • What? Won't that work? It's even Vanilla JS. @SpikeX Commented Jun 3, 2013 at 17:48
  • @11684 OP wants to take only the word 'initial' from within the <div> he's posted, and replace it with a text box. So the resulting HTML should be <div>This is my <input type="text" /> text</div>. Commented Jun 3, 2013 at 17:53
  • The code in my comment produces a string ("<input type=\"text\" />"). The only reason why the OP's code didn't work was that he was trying to use jQuery object instead of a string. @SpikeX Commented Jun 3, 2013 at 18:31

4 Answers 4

5

Why not surround it with a span so that it's easier to find:

<div>This is my <span id="replace">initial</span> text</div>

Then:

$('#replace').replaceWith(input);
Sign up to request clarification or add additional context in comments.

2 Comments

That's pretty freaking amazing, three upvoters doesn't know that javascripts replace() method only works on strings, and not on jQuery elements ?
@adeneo Ha! Good catch, should have been .replaceWith(). Fixed!
2

Try:

$("div").html($('div').text().replace('initial',input.html()));

Though I like this better if your jQuery is new enough:

$("div").html($.parseHTML($('div').text().replace('initial',input.html())));

3 Comments

You like the longer one better...?
Um, that going to say something like [Object object]
@SpikeX The value passed to the .html() function begins with plain text and not not text containing a tag--like <div>...
0
var input = $("#myinput");
$("div:contains(initial)").replaceWith(input);

FIDDLE

EDIT:

to replace just the word "initial" :

var input = $("#myinput");
$("div:contains(initial)").html(function(_,html) {
     return html.replace('initial', input.get(0).outerHTML);
});

FIDDLE

5 Comments

How many times do I have to post this comment in this thread: I believe he only wants to replace the word "initial", not the whole thing.
@SpikeX - That's even easier ?
I wouldn't call it "easier" if it's three lines instead of two. ;)
Maybe not easier, but not very hard either, and I did'nt even add elements to the markup.
Great! outer.HTML is what I was looking for. Thanks
-2

This ought to work:

$("div").html(input)

EDIT: actually, no, it won't, you need to wrap "initial" with some sort of element, preferably with an "id" or "class" element so you can select it directly.

EDIT #2:

// Grab the actual HTML of the element you want to inject
var elementHTML = $(input).html();

// Loop over each DIV, though you really should use ID/class attributes
$("div").each(function() {
    var divHTML = $(this).html(); // Grab a copy of the DIV's HTML
    $(this).html(divHTML.replace('initial', elementHTML)); // Replace "initial" in the saved string with the element's HTML from above
});

8 Comments

I believe he only wants to replace the word "initial", not the whole thing.
This won't preserve the rest of the string.
Yeah, but you've got to remember that if someone comes across this post in the future, they should only see answers that are correct / helpful. The "proper" thing to do would be to delete and if you find another solution, post another answer at that time. I delete my answers all the time if I'm wrong, it's not about pride.
True, but deletion isn't mandatory either (though it's likely that wrong answers will continue to garner down-votes as time goes on, so non-deletion becomes masochistic after a while).
Meh, they're imaginary points. If I can't come up with a solution, I'll delete a post. If, on the other hand, like here, I come up with a correct solution, it might be helpful to see the thought process in figuring out the correct answer.
|

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.