Populating a radiogroup programatically

  • baileynp
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 76
  • Thank you received: 1

baileynp created the topic: Populating a radiogroup programatically

Hi

I have a test form which consists of: 10 textfields named txt_grammar_x (where x ranges from 1 to 10), one radiogroup named rg_grammar_1, and a submit button 'check' which has the following code:
function ff_btn_check_action(element, action)
{
    switch (action) {
        case 'click':
					jQuery.ajax({                              
						type: 'GET',	
						dataType: 'json',			
						url: 'placement_test.php',
						data:{'test_id':1},
						async:false,
						success: function(data) {
							var obj = JSON.parse(JSON.stringify(data));
							var arr = obj;
							for (var s = 1; s <=2; s++) {
								var section;
								var count;
								if (s == 1) {
									section = 'grammar';
									count = 10;
								} else {
									section = 'vocabulary';
									count = 10;
								} // if
								for (var q = 1; q <= count; q++) {
									var q_name = 'txt_' + section + '_' + q;
									var a_name = 'rg_' + section + '_' + q;
									if (arr[q - 1][0] == s) {
										ff_getElementByName(q_name).value = arr[q - 1][1];
										var answer = arr[q - 1][2] + '\n' + arr[q - 1][3] + '\n' + arr[q - 1][4] + '\n' + arr[q - 1][5];
										ff_getElementByName(a_name).value = answer;
									} // if
								} // for q
							} // for s
						} // function(data)
					}); // jQuery.ajax
            break;
        default:;
    } // switch
} // ff_btn_check_action

As you can see, the code makes a call to a php file to connect to my database, retrieve and return data - this all works perfectly. The problem is when I am trying to populate the form's objects with the returned data.

The line which populates the textfields

ff_getElementByName(q_name).value = arr[q - 1][1];

works ok, but the line ff_getElementByName(a_name).value = answer; doesn't populate the radiogroup, and returns the error

null is not an object (evaluating 'ff_getElementByName(a_name).value=answer')


What am I doing wrong here?

Regards
#237297
  • baileynp
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 76
  • Thank you received: 1

baileynp replied the topic: Populating a radiogroup programatically

Hi - any ideas what I'm doing wrong here?

I'm sure it's a simple, but stupid mistake I'm making. :)

I simply want to populate the radiogroup, but f_getElementByName(radiogroup_name).value = formatted_text_with_line_feeds; doesn't work. :( :(
#237381
  • Posts: 5958
  • Karma: 119
  • Thank you received: 554

MarioPuco replied the topic: Populating a radiogroup programatically

Hi,

Replace your line that sets the radio group value with this piece of code:

var rgs = document.querySelectorAll('[name="ff_nm_' + a_name + '[]"]');
               rgs.forEach(rg => {
                if (rg.value == answer) {
                rg.checked = true;
           }
  });

Let me know if this works.
Regards,
Mario

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

baileynp replied the topic: Populating a radiogroup programatically

Hi

I'm not trying to set the radiogroup to checked. I want to populate the radiogroup's values, e.g.

0;text1;0
0;text2;1
0;text3;0
0;text4;0

I've already retrieved the data and formatted the data with linefeeds as above, but I'm stuck in how to populate the radiogroups in the code. :(

Please, please help. :)
#237385
  • Posts: 5958
  • Karma: 119
  • Thank you received: 554

MarioPuco replied the topic: Populating a radiogroup programatically

Hi,

Can you send a URL to your form please ?

Regards,
Mario

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

baileynp replied the topic: Populating a radiogroup programatically

Sure :)

This message contains confidential information


The textfields are populated by looping through a recordset, but the problem I have is with the radiogroups. I want to replace Answer 1, Answer 2, Answer 3, and Answer 4 with the values from the recordset.

#237389
Attachments:
Moderators: ForumSupporttomeperica
Time to create page: 0.220 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