Why does .replaceWith() not work in the following code?
After clicking the radio button, the HTML between the <form> is removed.
$('input:radio').click( function(){
var url = $(this).attr('alt');
$('form').replaceWith($('<form action="'+url+'" method="post" class="url">' + this.innerHTML + '</form>'));
});
Example: http://jsfiddle.net/MWFN9/1/
I think what you’re trying to do is change the value of the
actionattribute when a radio button is clicked.replaceWithreplaces the entire element (and it’s children) which is why your entire form is replaced (with an empty one).Instead, you can use
attrto change the value of a single attribute: