Replace HTML Code

I have this code that I need to insert/replace with a surround

tag.

<div class="formBody"><input type="radio" id="Sex0" value="Male" name="form[Sex]"><label for="Sex0">Male</label><input type="radio" id="Sex1" value="Female" name="form[Sex]"><label for="Sex1">Female</label><span class="formValidation"><span class="formNoError" id="component1956">Male/Female</span></span></div>

I need to add this following surrounding <div class="ck-button"> code using jQuery.

<div class="formBody"><div csass="ck-button"><input type="radio" id="Sex0" value="Male" name="form[Sex]"><label for="Sex0">Male</label></div><div class="ck-button"><input type="radio" id="Sex1" value="Female" name="form[Sex]"><label for="Sex1">Female</label></div><span class="formValidation"><span class="formNoError" id="component1956">Male/Female</span></span></div>

I’ve tried using ‘replaceWith’ but not having any luck as the code I’m using is literal. For example:

jQuery( '<input type=\"radio\" id=\"Sex0\" value=\"Male\" name=\"form[Sex]\"><label for=\"Sex0\">Male</label>' ).replaceWith( '<div csass=\"ck-button\"><input type=\"radio\" id=\"Sex0\" value=\"Male\" name=\"form[Sex]\"><label for=\"Sex0\">Male</label></div><div class=\"ck-button\"><input type=\"radio\" id=\"Sex1\" value=\"Female\" name=\"form[Sex]\"><label for=\"Sex1\">Female</label></div>' );

So where am I going wrong and how do I fix it?

In your jQuery(), you are using apostrophe ' as the string delimiter, but then you escape the double quotes within \". Since the apostrophe and double quotes are not the same, you don’t need to escape the double quotes - that is only necessary if you are using one or the other for both string delimiter and attribute value. Remove the backslashes and it should work fine, I think.

HTH,

:slight_smile:

There’s no change. You can see that in my screenshot.

Uh… hmm… well, I see that you’re using the command jQuery(), but I don’t see you indicating that this change should happen anywhere in the DOM. You’re giving it code to replace, and the code to replace it with, but it doesn’t have a target.

HTH,

:slight_smile:

And that’s key.
Once the proper target is obtained it will be a lot easier to create / insert / replace new elements.

Thanks, gents. Looks like I have a lot more to learn.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.