I'm creating a profile page that changes all <strong>Title</strong>
tags into <input type="text" value="Title">
and <p>Content</p>
tags into <textarea>Content</textarea>
tags.
This works great with this:
$(this).html(function(i, h) {
return h
.replace(/<strong>/g, "<input type=\"text\" value=\"")
.replace(/<\/strong>/g, "\" class=\"profile-header\">")
.replace(/<p>/g, "<textarea>")
.replace(/<\/p>/g, "</textarea>");
});
However, I need an ID for each set of <strong>
and <p>
tags (as they represent different parts of the profile). The issue I'm having is adding an ID for these, which I have put on the <strong>
tags like this: <strong data-id="0">
but can't work out why the regular expression I'm using doesn't work properly.
Here's what I have so far, hopefully you can see what I'm trying to achieve:
$(this).html(function(i, h) {
return h
.replace(/<strong data-id=\"(\i+)\">/g, "<input $1 type=\"text\" value=\"")
.replace(/<\/strong>/g, "\" class=\"profile-header\">")
.replace(/<p>/g, "<textarea>")
.replace(/<\/p>/g, "</textarea>");
});
And just for reference, this is how I am returning the HTML back to its original state (and how the data is saved):
$(".column.about input").each(function() {
// Get the titles and content from the inputs and textareas
var content_id = $(this).data("id");
var title = $(this).val();
var content = $(this).next().val();
// We're changing the content first so we don't loose $(this)
$(this).next().after("<p>" + content + "</p>").remove();
$(this).after("<strong data-id=\"" + content_id + "\">" + title + "</strong>").remove();
$.post("../includes/ajax.php", { action: "updateProfile", section: "about", id: content_id, title: title, content: content }).done(function(data) {
if(data != "saved") {
throwErrorMessage(data);
}
});
});