Populating a radiogroup programatically

  • Posts: 5896
  • Karma: 118
  • Thank you received: 544

MarioPuco replied the topic: Populating a radiogroup programatically

Hi,

// You can't just overwrite the input element with the data as you would from BreezingForms backend.
// You have to manually change the value and innerHTML of those radio buttons.
// You can use i as the counter for radio group elements, so since your PHP returns an array which has 6 array elements each,
// the strings for the radio group elements are 3, 4, 5 and 6.
// Split them by ';' and use the second element (index = 1) for the innerHTML and the third element (index = 2) for the value.
var rgs = document.querySelectorAll('[name="ff_nm_' + a_name + '[]"]');
rgs.forEach((rg, i) => {
rg.value =
// get the value part of the string 0;text;value, split it by ';' and take the last part
rg.innerHTML = // get the text part of the string
});

Rgds,
Mario

Satisfied with our Support ?
PayPal Tip
===============================================
Need to renew Pro Support?
Buy Here!
===============================================
Satisfied?
Consider a membership!
===============================================
Like us on Facebook
===============================================
#237391
  • baileynp
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 54
  • Thank you received: 1

baileynp replied the topic: Populating a radiogroup programatically

Hi

Thanks for the reply, but I'm not exactly sure what you mean. :(

Could you possibly give me an example?

Thanks
#237392
  • baileynp
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 54
  • Thank you received: 1

baileynp replied the topic: Populating a radiogroup programatically

Hi Mario

Darko suggested I used the following code:
for (x = 0; x < arr.length; x++) {
    // Populate the radiogroups (answers)
    var rgs = document.querySelectorAll('input[name="ff_nm_' + elem_a + '[]"]');
    var ffelem = rgs[0].getAttribute('id');
    rgs.forEach((rg, p) => {
        rg.value = arr[x][2*p + 4];
        jQuery('label[for*="' + ffelem + '"]')[p].html(arr[x][2*p + 3]);
    }); // forEach
} // for x

In this code he used the undefined variable 'p', which I added as:
for (x = 0; x < arr.length; x++) {
    // Populate the radiogroups (answers)
    var rgs = document.querySelectorAll('input[name="ff_nm_' + elem_a + '[]"]');
    var ffelem = rgs[0].getAttribute('id');
    [b]var p = 1;[/b]
    rgs.forEach((rg, p) => {
        rg.value = arr[x][2*p + 4];
        jQuery('label[for*="' + ffelem + '"]')[p].html(arr[x][2*p + 3]);
        [b]p++;[/b]
    }); // forEach
} // for x

Unfortunately, the result is the same - the correct data is returned from the array, but rg.value = arr[x][2*p + 4]; doesn't change any of the radiogroup values, and the jQuery line generates the error:

jQuery('label[for*="'+ffelem+'"]')[p].html is not a function. (In 'jQuery('label[for*="'+ffelem+'"]')[p].html(arr[x][2*p+3])', 'jQuery('label[for*="'+ffelem+'"]')[p].html' is undefined)


Please can you take a look?

Thanks
#237394
  • baileynp
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 54
  • Thank you received: 1

baileynp replied the topic: Populating a radiogroup programatically

Update - I've got it working!!! :) :)

The code Darko supplied didn't like the line:
jQuery('label[for*="' + ffelem + '"]')[p].html(arr[x][2*p + 3]);

I've got it working with a fudge which is:
for (var i = 0; i < arr.length; i++) {
...
	// Populate the radiogroups (answers)
	var rgs = document.querySelectorAll('input[name="ff_nm_rg_' + section + '_' + arr[i][1] + '[]"]');
	var elem_id = rgs[0].getAttribute('id');
	var p = 1;
	rgs.forEach((rg, p) => {
		// Set the RG value
		rg.value = arr[i][2*p + 3];
		// Set the RG label
		jQuery(document).ready(function(){
			if (p == 0) {
				jQuery("label[for=" + elem_id + "]").html(arr[i][2*p + 4]);
			} else {
				var elem_ext = '_' + (p);
				jQuery("label[for=" + elem_id + elem_ext + "]").html(arr[i][2*p + 4]);
			} // if
		});
	p++;
	}); // forEach
} // for i

but if you could get Darko's code working for me, that would be a more elegant solution. :)

Thanks again.
#237401
  • Posts: 5896
  • Karma: 118
  • Thank you received: 544

MarioPuco replied the topic: Populating a radiogroup programatically

Hey,

Glad you made it work.
I'll let Darko know but I cannot promise anything !

Regards,
Mario

Satisfied with our Support ?
PayPal Tip
===============================================
Need to renew Pro Support?
Buy Here!
===============================================
Satisfied?
Consider a membership!
===============================================
Like us on Facebook
===============================================
#237405
Moderators: ForumSupporttomeperica
Time to create page: 0.038 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here