Q:

replace in jQuery

Can some one help me in this
I had a variable 
var myValue = "what is you name? what is your age?"

i want to find the '?' in the string and replace it with a html input text element
where the user can enter the answer in the text box and at last i need a string as out put like this
"what is your name my name is xyz what is your age i am 25"
Please help me in this
Thanks
Kumar

A:

This will dynamically take your myValue, replace all ?'s with inputs and then add a button to alert you of the user input. It will place myValue into the body, but you can place it somewhere else.
$(function() {
  var myValue = "what is you name? what is your age?";
  myValue = "<div>" + myValue;
  while(myValue.indexOf("?") > -1)
    myValue = myValue.replace("?", " <input type=\"text\" />");
  myValue += "</div>" + "<button type=\"button\" onclick=\"sumUp(this)\">Declare</button>";
  $("body").html(myValue);
});

function sumUp(button) {
  var $prevDiv = $(button).prev().clone();
  $prevDiv.children("input").each(function() {
    $(this).replaceWith($(this).val());
  });
  alert($prevDiv.html());
}

