July 28, 201115 yr I'm making a form that changes options when eg a selection menu has a certain value. The weird thing is that it 'works' in terms of 'the fields that should be displayed are displayed'...however when I use the line below.. $('.item3 .rightbtn').html('<input type="text" name="sBla" value="bla" />'); ..the form field sBla will always stay 'bla' even if I change it's content..have tried to remove the value property but that didn't help either, even if I type something inside it stays empty...what am I doing wrong? cheers
July 28, 201115 yr Pedro is right about using .val() but I'm not really sure what the problem is. I created a test using your jquery code above and it worked as i would have expected to: <script type="text/javascript"> function testing(){ $("#test").html($("input[type='text'][name='sBla']").val()); } $(document).ready(function(){ $('.item3 .rightbtn').html('<input type="text" name="sBla" value="bla" />'); }); </script> <div class="item3"> <span class="rightbtn"></span> <input type="button" onclick="javascript:testing();">testing</input> </div> <div id="test"></div> If you type "this is a test" into the input box then click the testing button it inserts "this is a test" into the div. What are you trying to do with it for it not to be working as you would expect? Edited July 28, 201115 yr by ysuran
July 28, 201115 yr Author If you want to change the value, it's best to use val() instead of html() I'll give that a try when I get back thanks. Pedro is right about using .val() but I'm not really sure what the problem is. I created a test using your jquery code above and it worked as i would have expected to: <script type="text/javascript"> function testing(){ $("#test").html($("input[type='text'][name='sBla']").val()); } $(document).ready(function(){ $('.item3 .rightbtn').html('<input type="text" name="sBla" value="bla" />'); }); </script> <div class="item3"> <span class="rightbtn"></span> <input type="button" onclick="javascript:testing();">testing</input> </div> <div id="test"></div> If you type "this is a test" into the input box then click the testing button it inserts "this is a test" into the div. What are you trying to do with it for it not to be working as you would expect? It's the last part of the form, after I submit the form the value will always be empty...with my piece of code the sBla field does appear, that's not the problem, but when form is submitted $_POST['sBla'] will always have the value of in this case 'bla' no matter what I type into the field. Thanks though for your effort in writing the code.
July 28, 201115 yr ok rewritten through a form submit and it still works: <?php If(isset($_POST["sBla"])){ $sBla = $_POST["sBla"]; }else{ $sBla = ""; } ?> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> </head> <body> <form method="post"> <div class="item3"> <span class="rightbtn"></span> <input type="submit"/> </div> </form> <div id="#test"><?php echo $sBla; ?></div> <script type="text/javascript"> $(document).ready(function(){ $('.item3 .rightbtn').html('<input type="text" name="sBla" value="bla" />'); /*$newinput = $("<input />").attr({type:'text',name:'sBla'}).val('bla'); $('.item3 .rightbtn').append($newinput);*/ // this is just another way of writing the input element }); </script> </body> </html> any chance you could post some more of your code to see if it's something else causing the problem? Edited July 28, 201115 yr by ysuran
Create an account or sign in to comment