Skip to main content

Some form values not getting submitted after using .replaceWith()



Let's say I have this form:







<form action="submit" method="post">

<select name="category" id="categorylist">

<option value="love">Love</option>

<option value="magic">Magic</option>

<option value="custom">Custom</option>

</select>

<textarea name="content">SomeContent</textarea>

<input type="submit">

</form>







I wanted to change the select into an input when I select custom so I came up with this:







$(function(){

$('#categorylist').change(function(){

$(this).replaceWith('<input type="text" name="category">');

});

if( $('#categorylist').val() == 'custom' )

$('#categorylist').replaceWith('<input type="text" name="category">');

});







But when the select is changed into an input, $_POST['category'] isn't there when i dumped $_POST on form submission. Why is it so?


Comments

  1. You should replace the <select> with </select> and <input type="submit> to <input type="submit"> in your html. With following javascript there is no problem with .replaceWith() and submitting form.

    $(function(){
    $('#categorylist').change(function(){
    if( $('#categorylist').val() == 'custom' )
    $('#categorylist').replaceWith('<input type="text" name="category">');
    });
    });


    See my test.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?