replaceWith error

I wonder if somebody can help me please. I’ve got this code, but it doesn’t work. I knw it’s stuck on the ‘$($newName).replaceWith(’<div…’ line but I don’t know how to fix it.

Thanks in advance

<script type="text/javascript"> $('#save').on('click', function(event) { var $newName = $(this).attr('id'); $newName = $newName.replace("div_name","div_new_name"); $($newName).replaceWith('<div style="display: block;" id="'+ $newName +'">This is new div content</div>'); }); </script>

What’s you’re intention here, are you trying to replace the element with that id with the new content?

If so you’ll need a valid id #selector, e.g.

$newName.replace("div_name","#div_new_name");

I want to use the actual element but doing it like that wasn’t working.

I couldn’t just completely replace the element because there’s also a unique number at the end of it (is div_name_X)

I changed the code to this in the end and it now works:
$("#div_name"+$newName).

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