0

There is HTML as a string returned by Ajax call.

headings
....
<div class="wrapper">
    <input type="text" name="email" value="" />
</div>
....

I'm retrieving the .wrapper and input element

var elements = $(MY_HTML_STRING);
var domName = $('input[name="name"]', elements);

then making updates with both of them, and, my question is how to put them back into MY_HTML_STRING for further sending with ajax to different recipient.

Please let me know if it will be better to do with regex.

UPDATE:

Updates are: input value updating + .wrapper class updates and writing a piece of data into global variable. Everything can be done with regex, but I like the jQuery smoothness of code. The question is technically: if it's possible in the way I suggested.

2
  • 2
    It's a little unclear what you're asking. What kind of updates are you making to them and what are they when you're finished updating them? Commented Feb 21, 2014 at 22:06
  • Please check my updates. Commented Feb 21, 2014 at 22:11

1 Answer 1

1

After you've made your updates you can simply do something like the following (assuming there is only one div.wrapper that you're working with and that it is unique):

MY_HTML_STRING.replace(/<div class="wrapper">.*?<\/div>/, $('.wrapper').html());

Then you can make another AJAX call and pass the string back to the server.

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

2 Comments

Great answer, but i'm making .wrapper class updates, while $('.wrapper').html() return inner HTML. Lets forget about the .wrapper for now. Is there a way to update input value and put it back into the HTML string not using regex?
1. You could do $('.wrapper').clone().wrap('<div/>').parent().html() to include the wrapper HTML in a "hackish" sense. 2. Short answer, not really. If the original inner contents may vary then RegEx is the only solution. Why would you be strongly opposed against RegEx though? It's awesome!

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.