• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Stumped on seemingly simple javascript error

porlhews

Member
Hi All,

Anyone got any pointers as to why this custom javascript wont work:

Code:
var adult = this.get('value');
if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update('newvalue') ;}

In chrome, the consol says "Uncaught SyntaxError: Unexpected identifier "
Joomla 3.3
Fabrik 3.2
Php 5.5.18

Thanks in advance,

Paul Hughes
 
Hi All,

Anyone got any pointers as to why this custom javascript wont work:

Code:
var adult = this.get('value');
if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update('newvalue') ;}

In chrome, the consol says "Uncaught SyntaxError: Unexpected identifier "
Joomla 3.3
Fabrik 3.2
Php 5.5.18

Thanks in advance,

Paul Hughes
Is your form id 1? If not you need to change the getBlock('form_1') to the corrcet form id.

Also, the sample you provided from the Wiki updates the element's value with the string 'newval'.
What you put as 'newval' should be the replacement value (or if values, an array of values separated by '|-|', as specified in the Wiki link you provided) - and those must be of the same var type as the original value they are to replace. e.g. .update(1), .update('1'), .update(true), .update( 675.55) . Also, keep in mind, if the value passed to update() is a variable - e.g. .update($yourVar) , then that variable will be sent as that variable's value and type.
 
Hi,

Thanks for your thoughts.

The form ID is correct (1).

Both elements are fields (I.e.. Text fields) not sure if I'm presenting the 'newvalue' correctly?

At the moment I just want to fill in count_checkbox with the word newvalue.

Any ideas?

Thanks again for your help.
 
Hi,

I've just tried .update(newvalue), .update('newvalue') and .update("newvalue")

All of them give the same error?
 
Hi,

I've just tried .update(newvalue), .update('newvalue') and .update("newvalue")

All of them give the same error?
In your code send an alert, or to console.log, the value of your element and check what variable type and format it is in. Then duplicate that in what you send with update();
e.g.

Code:
var thisVal = Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox');
 
alert('var type is ' + typeof(thisVal) + ' and count_checkbox is ' + thisVal);

From there you should know the expected format of what you need to use as value in your update(value).
 
Hi Bauer,

Thanks for your help, but I still get the same error (Uncaught SyntaxError: Unexpected identifier).

Looking closer at the console, the error appears to be associated with the preceding element's javascript. I've copied the javascript console in below (the error has underlined the line I've highlighted in red). However when I disable that preceding element, the error is now associated with the preceding error again. Furthermore, when I disable my count_checkbox element, no errors appear at all.

Fabrik.blocks['form_1'].dispatchEvent('dropdown', 'jos_perm___Internornot2', 'load', 'if (this.get(\'value\') == \'Domestic\') {Fabrik.blocks[\'form_1\'].doElementFX(\'fabrik_trigger_group_group52\', \'hide\', this)}');

Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var adult = this.get('value');if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update('newvalue') ;}');

Not really sure where to go with this one. Any ideas more than welcome, thanks again for your help.

Paul
 
The code you show highlighted in red is missing a closing single quote in the last parameter for the dispatchEvent function.

Fabrik.blocks['form_1'].dispatchEvent('dropdown', 'jos_perm___Internornot2', 'load', 'if (this.get(\'value\') == \'Domestic\')

Each dispatchEvent('param', '...' , '...' ) is surrounded by single quotes - but the last param only has a quote before the if - but not at the end just before the closing parenthesis.
 
Bauer you are fantastic - I'm relieved to at least know the cause of the problem.

Unfortunately , I suspect this is a bug in fabrikar as that JavaScript code was generated using the built in dropdown options.

I'll report this to fabrikar and keep you posted where we get to.

Thanks again :)
 
Ooops. Well actually I was wrong. Re-examining that line and the syntax is fine after all.:confused:
The rest of that was all part of the if - and it has a closing single quote after all.
Sorry for the misdirection.

Now I really feel obligated.

This is your code - right?
Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var adult = this.get('value');if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update('newvalue') ;}');

From what I see - you are passing the string 'newvalue' as the update value when you should be passing the value of the variable adult (assuming that is your intent, right?).

So try this...
Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var adult = this.get('value');if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update(adult) ;}');

The only issue I see - even with that change, is - if the update field is a string and you are passing a number (the number 1 rather than the string '1') and trying to update a string with a number - then you will also get an error.

What I was trying to get at earlier is that you need to know if the var 'jos_perm___count_checkbox' is seen as a number or a string. And the problem you may have with your javascript is - when you assign the value of 'jos_perm___Number_of_adults_required' to the var 'adult' - is that being initialized by javascript as a number or a string value?

You never told us if jos_perm___count_checkbox and jos_perm___Number_of_adults_required are BOTH the same variable type. To save any headaches you might encounter (requiring the need for a var type conversion of number to string or string to number), they MUST be the same variable type in order for that last code change to work.

If that is not the case, (and you can't change the var type to be the same for both jos_perm___count_checkbox and jos_perm___Number_of_adults_required) then try either of these instead...

If 'jos_perm___count_checkbox' value is expected as a string, try...

Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var adult = this.get('value');if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update('1') ;}');

-or- you could assure 'adult' is a string by using the toString() function;

Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var aval = this.get('value'); var adult = aval.toString(); if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update(adult) ;}');

But if 'jos_perm___count_checkbox' value is expected as a number, try...

Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var adult = this.get('value');if(adult == '1'){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update(1) ;}');

-or- you could assure 'adult' is a number by using the parseInt() function;

Fabrik.blocks['form_1'].dispatchEvent('field', 'jos_perm___Number_of_adults_required', 'click', 'var aval = this.get('value'); var adult = parseInt(aval); if(adult == 1){Fabrik.getBlock('form_1').elements.get('jos_perm___count_checkbox').update(adult) ;}');

If none of those work then I'm lost.
 
Hi Bauer,

Thanks for your continued help.

With respect to the type of elements; they are both 'fields' created in Fabrik.

I checked my MYSQL database, they are both VARCHAR(255)

I tried all four of your suggestions, they didn't work.

Something that did just occur to me, when this field is loaded, it is empty. Do you think trying to look up the value of an empty field is causing a problem somewhere?

Thanks again for your help.

Paul
 
No that wouldn't be a problem - except that it is empty so it will never be '1' when a new row is added!
I forgot that when a new row is added the value will be blank - unless the element is the auto-incrimented primary key, or the element is something like the 'user' plugin, or has a 'default' value assigned to it.

A better understanding of just what you're trying to do here will go a lot further to help.

If both of those elements will always be numeric integers, then I would use the 'Formatting' tab of both elements to change the field type to be 'Integer'. (the default size limit for an integer in that Formatting tab is 11 - or up to 99,999,999,999 - but you can lower that if appropriate)

Then, by assigning a default value of 1 (no quotes) to both the jos_perm___count_checkbox and the jos_perm___count_checkbox elements , they will never be blank, even for a new added row (both would start off with a value of 1).

I supppose my big question is, if I'm understanding what you want to do (i.e. change the value of jos_perm___count_checkbox to 1 if jos_perm___count_checkbox elements is 1 ???) - why would you use an on 'click' event? A more appropriate action might be to use the on 'change' event.

Furthermore - if you want to force the user, when adding a new row, to enter a value for jos_perm___Number_of_adults_required, then set the default value to 0 instead of 1 - and add a validation rule saying that field must be >0. This way the user is forced to change the value and your javascript on 'change' code will get triggered when they do make the change.

But then, the question is - should anything be done to change the value of jos_perm___count_checkbox back to something else if the value of jos_perm___Number_of_adults_required is later edited to change it back to something other than 1?

Programming logic. Don't you love it.;)

I tried (a varient of) the code I suggested on a test table and it works for me.
Wait a minute - Are we both talking about the same place for entering javascript???
I'm adding this to the javascript in the element's configuration - NOT the form javascript plugin.

For example I have a table with fields named test_list_joined___test_joined_field1 and test_list_joined___test_joined_field2 - both are 'field' elements set as Integer format.

In the 'Javscript' settings for element test_list_joined___test_joined_field1...

Event: change
Javascript code:
var thisVal = this.get('value');
if(thisVal == '1'){
Fabrik.getBlock('form_143').elements.get('test_list_joined___test_joined_field2').update(thisVal);
}

This code works fine for me - to replace the value of test_list_joined___test_joined_field2 with the value of test_list_joined___test_joined_field1 whenever test_list_joined___test_joined_field1 is changed.
 
Hey Bauer,

Thank you so much for your help, I've managed to resolve the problem. You're not going to belive it - the problem was the use of single quotes. I changed from single to double quotes and it works fine.

var adult = this.get("value");
if(adult == "1"){Fabrik.getBlock("form_1").elements.get("jos_perm___count_checkbox").update(1) ;}

Thank you so much for your time.

Paul

P.s., apologies for the shameless request, but if you fancy another challenge; I'm struggling with counting checkboxes; http://fabrikar.com/forums/index.php?threads/count-selected-checkboxes.39883/
 
When it comes to programming mysteries and quirks - I believe anything.;)
Just glad you got it working.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top