2

my html

<div id="sample-reminder">
    <div id="reminder_0000">
        <input type="text" name="val_0000" value="1">
    </div>
</div>

my code

rigaHtml = $('#sample-reminder').clone(); // i have to work on a clone of dom
rigaHtml.find('input[name^=val_]').val(99);
rigaHtml.html().replace(/0000/g, 987654321); 

last command not replace my placeholder '0000'. if i move replace() before find(), i cant' use find :-(

0

4 Answers 4

2

You don't need to use .clone() in this case:

var rigaHtml = $('#sample-reminder').html();
$(rigaHtml.replace(/0000/g, 987654321))
  .find('input[name^=val_]')
  .val(99)
  .appendTo('#container')

Where '#container' is the node you wish to add the modified HTML to.

Sign up to request clarification or add additional context in comments.

2 Comments

I assumed there was a reason OP said i have to work on a clone. Seems you knew more than the rest regarding OPs intentions :) +1.
@FrançoisWahl This is because OP worked iteratively; at first a clone would be required, otherwise the .val() would update the sample div (which is hidden); the .replace() came later, but by that time a new approach was needed :)
1

Assuming you are looking to change the id property of #reminder_0000 and the name property of val_0000, try this:

$rigaHtml = $('#sample-reminder').clone();
var $input = $("input", $rigaHtml);
var $div = $input("div", $rigaHtml);

$input.val(99).attr("name", $input.attr("name").replace(/0000/g, 987654321));
$div.attr("id", $div.attr("id").replace(/0000/g, 987654321));

Comments

0

You need to set the result back.

var html = rigaHtml.html().replace(/0000/g, 987654321);
rigaHtml.html(html);

Comments

0

You are not doing anything with the returned value of replace. That should be written as:

rigaHtml.html(rigaHtml.html().replace(/0000/g, 987654321)); 

Even then, rigaHtml is not in the DOM because it's a clone of the original elements. So you still wouldn't see a visible change unless you put it back inside the DOM.

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.