Get an element value that is inside a repeat group

gnavarro

Member
Hi,

How can I get an element value that is inside a repeat group to update an element in the same form using JS?

This code is not working
Code:
var morada = Fabrik.getBlock('form_9').formElements.get('fb_locais_tratamento___morada').getValue();

fb_pacientes___morada_fisc.value=morada;

Thanks in advance for your help
 
Try:
Code:
var morada = Fabrik.getBlock('form_9').formElements.get('fb_locais_tratamento___morada').getValue();

document.getElementById("fb_pacientes___morada_fisc").value=morada;
or even:
Code:
document.getElementById("fb_pacientes___morada_fisc").value = document.getElementById("fb_locais_tratamento___morada").value;
 
Thanks Sophist for your quick reply

But not working :(

I'm putting the code in "fb_locais_tratamento___morada" element JavaScript that is inside a repeating group for the event "change"... I want to get this same element value and copy to fb_pacientes___morada_fisc that is in the same form (fb_pacientes)


What am I missing?
 
Sorry - I missed that you said it was in a repeat group.

I am not sure if this is the best approach, but this is how I would do it:

When you run the bespoke JS, "this" is set to the current element javascript object. So "this.element.id" should have the id - and this should have a repeat-group suffix e.g. "-2". So you subtract the element name, and get the suffix, and then add it to the other element name. Perhaps something like:

Code:
var repeat = this.element.id.slice("fb_locais_tratamento___morada".length);
document.getElementById("fb_pacientes___morada_fisc" + repeat).value = this.element.value;

But there might be a better way to do it if anyone else wants to chip in.
 
Well - I did say "perhaps" and "something like". You will need to experiment - or use a calc element to get the value instead of JS.
 
Humm didn't worked :(
Your problem is you're forgetting that every repeat group is part of an array of values.
Even if there is just one repeat, it is still an array - and the element ids have the array index included in them.

The first example in the Wiki under Developers - Javascript provides an example for specifically how to do this.
http://fabrikar.com/forums/index.php?wiki/javascript/

Another example using jQuery instead of pure javascript with Fabrik.getBlock - assuming you want to get the value from the 1st repeat group - would be...
JavaScript:
var morada = jQuery('#fb_locais_tratamento___morada_0').val();
jQuery("#fb_pacientes___morada_fisc").val(morada);
 
Your problem is you're forgetting that every repeat group is part of an array of values.
Even if there is just one repeat, it is still an array - and the element ids have the array index included in them.

The first example in the Wiki under Developers - Javascript provides an example for specifically how to do this.
http://fabrikar.com/forums/index.php?wiki/javascript/

Another example using jQuery instead of pure javascript with Fabrik.getBlock - assuming you want to get the value from the 1st repeat group - would be...
JavaScript:
var morada = jQuery('#fb_locais_tratamento___morada_0').val();
jQuery("#fb_pacientes___morada_fisc").val(morada);
That's it!:)
Thanks Bauer
 
No problem. Glad to help.
I used to hate javascript. Now I think jQuery is the greatest thing since sliced bread.:p
 
The answer to every question is not jQuery. Indeed in J4 jQuery is being phased out in favour of DOM HTML.

Bauer's solution will work only on the first row in the repeat group (if that is what you want). You did not say that the element you wanted to copy to was not in the repeat group. If so the simplest answer should be:
Code:
document.getElementById("fb_pacientes___morada_fisc").value = this.element.value;
which is the DOM HTML equivalent to Bauer's code except it works with more than one repeat group row and is completed in one Javascript statement without use of a library (rather than tens of statements spent inside jQuery).
 
The answer to every question is not jQuery. Indeed in J4 jQuery is being phased out in favour of DOM HTML.

Bauer's solution will work only on the first row in the repeat group (if that is what you want). You did not say that the element you wanted to copy to was not in the repeat group. If so the simplest answer should be:
Code:
document.getElementById("fb_pacientes___morada_fisc").value = this.element.value;
which is the DOM HTML equivalent to Bauer's code except it works with more than one repeat group row and is completed in one Javascript statement without use of a library (rather than tens of statements spent inside jQuery).
I never said that jQuery was the answer to every question.o_O

And I thought that I already explained that my solution was only for the 1st repeat group - and I provided that jQuery solution only AFTER I first (intentionally) provided a link to the Fabrik Wiki that gives a detailed example of exactly how gnavarro could solve his problem using 'plain' javascript and the built-in Fabrik javascript solutions.

Geez Sophist, if they didn't know better, people might think you have it out for me or something. All I'm trying to do here is help users in the Fabrik Community who are either newbies or, like myself, might not be able to afford upgraded support. If Hugh and Rob would rather I don't do that, I won't. All anyone has to do is tell me and I will cease and desist my attempts at support. I have very few good days where I am up to even doing that lately. Today just happened to be one of them.

That said, as far as I'm concerned, jQuery was never really "phased in" to Joomla. As I recall, MooTools was always their javascript library of choice. It's only since J3 and with the increased popularity of jQuery that more and more developers, including Fabrik, started including more and more jQuery solutions in their code. And yes, I realize every solution has an end life and we all can't keep up with the latest and greatest every time the wheel gets re-invented.

I made it known early on, when I first started using Fabrik and posting in this forum some 5 years ago now, that javascript was not a language that I had ever much worked with until then - and I just personally hated using it back then. I still do - and think it is very difficult to debug. Others who are longtime gurus with js might not see it that way - but I am only stating my opinion. If that's OK to do around here?:rolleyes:

So now I'm expecting that next you're going to be lecturing with me that a Mac is better than a PC or Linux is better than Windows?:p
 
I appreciate all the help and also that one that in the end could not be effective. I Think that the intention is what we should appreciate the most... So Sophist once again thank you very much for your quick reply in trying to help me and Bauer for arranging an solution... I appreciated your help in the same way ;)

By the way:) I want to apply the following condition and is not working:

Code:
var moradafisc_dif_loc_trat = fb_pacientes___moradafisc_dif_loc_trat.getValue();
if (moradafisc_dif_loc_trat === 0) {


var morada = jQuery('#fb_locais_tratamento___morada_0').val();
jQuery("#fb_pacientes___morada_fisc").val(morada);
jQuery("#fb_pacientes___morada_andar_fisc").val(morada);

}

fb_pacientes___moradafisc_dif_loc_trat is a yes/no element.

I've already tried with the following approaches:

  • document.getElementById("fb_pacientes___moradafisc_dif_loc_trat").getValue();
  • Fabrik.getBlock('form_9').formElements.get('fb_pacientes___moradafisc_dif_loc_trat').getValue();
  • "{fb_pacientes___moradafisc_dif_loc_trat}";

What am I missing?
 
Last edited:
Got it :)

Code:
var moradafisc_dif_loc_trat = Fabrik.getBlock('form_9').elements.get('fb_pacientes___moradafisc_dif_loc_trat').getValue();

if(moradafisc_dif_loc_trat === '0' ){

var morada = jQuery('#fb_locais_tratamento___morada_0').val();
jQuery("#fb_pacientes___morada_fisc").val(morada);
jQuery("#fb_pacientes___morada_andar_fisc").val(morada);

}

Man this is driving me nuts o_O Thanks any way...
 
Looks like you're getting the hang of it to me.:)

FYI - This is the jQuery-free version of that code (that is recommended by Sophist, and I'll try using in the future)...
JavaScript:
var moradafisc_dif_loc_trat = Fabrik.getBlock('form_9').elements.get('fb_pacientes___moradafisc_dif_loc_trat').getValue();

if(moradafisc_dif_loc_trat === '0' ){
    var morada = Fabrik.getBlock('form_9').elements.get('fb_locais_tratamento___morada_0').getValue();
    document.getElementById("fb_pacientes___morada_fisc").value = morada;
    document.getElementById("fb_pacientes___morada_andar_fisc").value = morada;
}
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top