Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

jQuery using html() to add input tag or select field

Featured Replies

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

If you want to change the value, it's best to use val() instead of html()

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 by ysuran

  • 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.

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 by ysuran

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.