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 - Dynamic Forms With Jquery Or Similar

Featured Replies

You know if jQuery (or similar) can do this....?

 

I have a <select> drop down with a number of options:

 

for example: green, blue, red, other

 

If the user chooses other, i want an <input> box to appear so they can type in their answer.

 

it probably can do this, but is there a tutorial on jQuery or anything anywhere, or does anyone know how to do this, it must be a fairly common problem.

you don't need jquery. Basic javascript can do that. Use css to hide the input box. You put a code on the onchange function of the first select so when it's value equals other it unhides the input box. Similiar to this. If you need more help just post. This hides a div but the principle is the same.

 

function checkHide(div_id, check_value, trigger_value)
{

var hiddenDiv = document.getElementById(div_id);

if (check_value == trigger_value)
{
	hiddenDiv.style.display = "block";	
}
else
{
	hiddenDiv.style.display = "none";	
}
return true;
}

verygood Daemon Byte.

 

If you want to keep it jQuery, heres how:

HTML

<select id="checkhide" checkfor="other" showid="colorinput">
<option>red</option>
<option>blue</option>
<option>other</option></select>

<input id="colorinput" />

 

jQuery:

$( function() {
 $("select#checkhide option").each( function() {
var checkfor = $(this).parent().attr("checkfor");
var showid	= $(this).parent().attr("showid");
var needle	= $(this).text();
if(needle == checkfor) {
  $("#"+showid).show();
} else {
   $("#"+showid).hide();
}
 });
});

 

This is probably the weirdest way to do it, but it requires no onclick / onchange in the select, nor any javvascript editing to make it work for different selects.

  • Author

awesome, i'll give it a go, if i get stuck ill post back.

 

cheers!

woops i made a mistake. code shoudl be this:

$( function() {
 $("select#checkhide option").click( function() {
var checkfor = $(this).parent().attr("checkfor");
var showid	= $(this).parent().attr("showid");
var needle	= $(this).text();
if(needle == checkfor) {
  $("#"+showid).show();
} else {
   $("#"+showid).hide();
}
 });
});

  • Author

I went with the Javascript version, rather than the jQuery one.

 

At the moment i have this:

 

		function showTextBox(Q6) {

		if ( Q6.options[Q6.selectedIndex].value == "15" ) {

			document.getElementById("Q6a").style.display = "inline";

		} else {

			document.getElementById("Q6a").style.display = "none";

		}
	}

 

and the select has an onchange function...

 

<select id="Q6" name="Q6" onchange="showTextBox(this)">

 

which will display the input field, if the drop down has the value of 15.

 

How do i alter this, so it shows an input box, when a checkbox is ticked?

change

 

if(Q6.options[Q6.selectedIndex].value == "15")

 

to

 

if(Q6.checked)

 

Assuming Q6 is the checkbox

jquery version:

function showTextBox( checkid, textid ) {
 if( $("#"+checkid ).attr("checked") == "checked" ) {
 $("#"+textid ).show();
 }
}

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.