0

This seems weird, but doing a jQuery replace() seems to only work when it's run in alert() or console.log.

Here's the code I'm trying to run:

$('.settings_item[data-slide-id=1324] .answers_fields_template li').each(function(index) {
  var regexp = new RegExp('new_answers', 'g');
  var new_id = new Date().getTime();
  $(this).html().replace(regexp, new_id);
});

And here's the html code:

<li data-weight="1">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_skeleton" name="survey[questions_attributes][0][answers_attributes][new_answers][skeleton]" type="hidden" value="true">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_text" name="survey[questions_attributes][0][answers_attributes][new_answers][text]" size="30" type="text" value="Great!">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_weight" name="survey[questions_attributes][0][answers_attributes][new_answers][weight]" type="hidden" value="3">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_response_class" name="survey[questions_attributes][0][answers_attributes][new_answers][response_class]" type="hidden" value="rate">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_id" name="survey[questions_attributes][0][answers_attributes][new_answers][id]" type="hidden">
</li>
<li data-weight="2">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_skeleton" name="survey[questions_attributes][0][answers_attributes][new_answers][skeleton]" type="hidden" value="true">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_text" name="survey[questions_attributes][0][answers_attributes][new_answers][text]" size="30" type="text" value="OK">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_weight" name="survey[questions_attributes][0][answers_attributes][new_answers][weight]" type="hidden" value="2">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_response_class" name="survey[questions_attributes][0][answers_attributes][new_answers][response_class]" type="hidden" value="rate">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_id" name="survey[questions_attributes][0][answers_attributes][new_answers][id]" type="hidden">
</li>

I'm trying to replace all instances of new_answers with the contents of the new_id variable.

When I run that, nothing gets replace. But, if I wrap $(this).html().replace(regexp, new_id); in alert() or console.log(), then the output of those shows new_answers being replaced as it should.

I can't figure out why on earth that'd be the case and now my brain hurts.

3
  • Is that just a quick-pasted code sample with accidental duplication or do you have all those duplicate IDs and NAMEs on your multiple inputs? If so, that will eventually cause you more headaches.
    – RwwL
    Jun 15, 2011 at 14:50
  • @RwL: That might have been me not reading things properly when editing. I see it's been rolled back and edited properly by the OP. Apologies to all.
    – Bojangles
    Jun 15, 2011 at 20:37
  • Weird, I still see the duplicate IDs.
    – RwwL
    Jun 16, 2011 at 14:55

3 Answers 3

3

You need to give the replaced HTML back to the element. Try this line:

$(this).html($(this).html().replace(regexp, new_id));

James

0

Are you trying to do a .replaceWith()? I do not see .replace() in the jquery API.

1
  • the .html() would return a string. .replace() is a method on the native string object...
    – ShaneBlake
    Jun 15, 2011 at 14:43
0
var newHtml = $(this).html().replace(regexp, new_id);
$(this).html(newHtml);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.