• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Dynamic dropdown values

Dynamic dropdown values 13 years 4 months ago #15597

I'm building a form that uses the dynamic dropdown script. If a use selects a certain country a second dropdown is dynamically populated only with states or providences found in that country.

Here's my situation. If for example a user selects United States. I would like the second dropdown to be populated with all fifty states spelled out...Alabama, Alaska, Arizona, etc, but I would like the submitted values to be the abbreviated forms, ie. AL, AK or AZ.

How do I alter the JavaScript code below to accomadate this?

<script type="text/javascript">
 
function dynamic1(parent,child){
 
	var parent_array = new Array();
 
	parent_array[''] = ['Please select a manufacturer'];
 
	parent_array['Audi'] = ['A3','A4','A5','A6','A8','Q5','Q7','S3','S4','S5','S6','S8','RS6'];
 
	parent_array['Dacia'] = ['Sandero','Logan'];
 
	parent_array['FIAT'] = ['Bravo','Punto','Grande Punto'];
 
	parent_array['Peugeot'] = ['207','308','407','607'];
 
	parent_array['SEAT'] = ['Ibiza','New Ibiza','Leon'];
 
	parent_array['Skoda'] = ['Fabia','Octavia Tour','Octavia 2','Superb'];
 
	var thechild = document.getElementById(child);
 
	thechild.options.length = 0;
 
	var parent_value = parent.options[parent.selectedIndex].value;
 
	if (!parent_array[parent_value]) parent_value = '';
 
	thechild.options.length = parent_array[parent_value].length;
 
	for(var i=0;i<parent_array[parent_value].length;i++){
 
		thechild.options[i].text = parent_array[parent_value][i];
 
		thechild.options[i].value = parent_array[parent_value][i];} }
 
</script>
The administrator has disabled public write access.

Re: Dynamic dropdown values 13 years 4 months ago #15698

Try to use the values like this:
AL|Alabama

This works in a dropdown list without javascript
Regards
Wilco Alsemgeest

Please note: my help is not official customer support. To receive your support, submit a ticket by clicking www.rsjoomla.com/my-support-tickets.html
The administrator has disabled public write access.

Re: Dynamic dropdown values 13 years 3 months ago #15878

  • learnthrusong
  • learnthrusong's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 26
  • Thank you received: 3
Hi there,

Yes, I believe that the way to go on this would be to follow @wilco.alsemgeest's suggestion.
// If you push the values into the array like;
parent_array['States'] = ['AL|Alabama','AK|Alaska','AZ|Arizona'];

Then your users will pick their state's FULL NAME from the select list and the value held in the form will be the SHORT CODE.

Hope this helps,

Gez
Last Edit: 13 years 3 months ago by learnthrusong.
The administrator has disabled public write access.

Re: Dynamic dropdown values 13 years 3 months ago #15986

  • ashley.barnard
  • ashley.barnard's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 33
  • Thank you received: 2
Hi I am looking at doing something similar where would the script go when i create the form field?

If you have a copy of the form to share that would be great.
The administrator has disabled public write access.

Re: Dynamic dropdown values 13 years 3 months ago #16011

  • learnthrusong
  • learnthrusong's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 26
  • Thank you received: 3
Hi Ashley,

Your script should go in the Javascript textarea (available in the CSS & Javascript area). Then...

On the form field that you want the function to run on, you'll need to add a call to your function using onclick or mouseover (whatever user action you want to instantiate your function) to the additional attributes of the field.

Hope this helps,

G
The administrator has disabled public write access.

Re: Dynamic dropdown values 13 years 3 months ago #16050

  • andreic
  • andreic's Avatar
  • OFFLINE
  • RSJoomla! Official Staff
  • Posts: 745
  • Thank you received: 66
Hello,

In order to make the pipe ( | ) character work in the Dynamic Drop-down change form example and have a value and label for all the elements of the dynamically generated dropdown please try to change the following code lines:

// Add the options
for(var i=0;i<parent_array[parent_value].length;i++)
{
thechild.options.text = parent_array[parent_value];
thechild.options.value = parent_array[parent_value];
}

with:

// Add the options
for(var i=0;i<parent_array[parent_value].length;i++)
{
var x = parent_array[parent_value].split('|');

thechild.options.text = x[1];
thechild.options.value = x[0];
}

Now you will be able to add the elements with both a value and label. For example instead of this line:

parent_array = ;

You can add this line:

parent_array = ;
Please note: my help is not official customer support. To receive your support, submit a ticket by clicking here
Regards,
RSJoomla! Development Team
The administrator has disabled public write access.

Re: Dynamic dropdown values 7 years 11 months ago #36860

Hello,

In order to make the pipe ( | ) character work in the Dynamic Drop-down change form example and have a value and label for all the elements of the dynamically generated dropdown please try to change the following code lines:

// Add the options
for(var i=0;i<parent_array[parent_value].length;i++)
{
thechild.options.text = parent_array[parent_value];
thechild.options.value = parent_array[parent_value];
}

with:

// Add the options
for(var i=0;i<parent_array[parent_value].length;i++)
{
var x = parent_array[parent_value].split('|');

thechild.options.text = x[1];
thechild.options.value = x[0];
}

Now you will be able to add the elements with both a value and label. For example instead of this line:

parent_array = ;

You can add this line:

parent_array = ;

Hi,
The method described above seems to work only in option.text: thechild.options.text = x[1];
I need to use the dynamic dropdown value in a calculation equation and in this case it doesn't work: (thechild.options.value = x[0];)
The value is always 0.
How can I fix this problem?
Thanks.
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!