You are viewing limited content. For full access, please sign in.

Question

Question

Change radio button after lookup completes

asked on July 18, 2019

I am trying to set a radio button based on a value in a single line field that has been populated from a lookup. I have only managed to get the radio button to be set if I click in the field and out again (triggering change).

I know this code is over the top, but I was trying to get one of the lookup triggers to work at least. Any help will be appreciated.

$(document).ready(function () {
  var a = '';
  $(document).on("onloadlookupfinished", function () {
    $('.filledField input').blur(function () {
      var value = $(this).val();
      $('.radio').find($('input[value="' + value + '"]')).attr('checked', true);
    });
  });
  $(document).on("lookupstarted",function(event) {
    $('.filledField input').blur(function () {
      var value = $(this).val();
      $('.radio').find($('input[value="' + value + '"]')).attr('checked', true);
    });
  });
  $(document).on("lookupcomplete",function(event) {
    $('.filledField input').blur(function () {
      var value = $(this).val();
      $('.radio').find($('input[value="' + value + '"]')).attr('checked', true);
    });
  });
});

0 0

Answer

SELECTED ANSWER
replied on July 19, 2019

I wouldn't bother using the lookup events, just attach a change event handler to the hidden field (unless you want it fire even when the value hasn't changed).

$(document).ready(function(){
    $('.filledField input').on('change',function(){
        // your code here
    });
});

Also, you can try .prop instead of .attr

$('your field').prop('checked', true).change();
0 0
replied on July 22, 2019

Hi Jason,

I did try that before. It only seems to trigger change if you actually type the value in. It doesn't work if the value is populated from a lookup.

 

Jono

0 0
replied on July 22, 2019 Show version history

That should not be the case.

I use change events triggered by lookups on several of my forms, and I believe the change event is necessary because it also triggers other things like validation.

Are you sure that the value was actually changing in your initial tests? if the value was already populated, or if it returned no results, then you would not get a change event.

0 0
replied on July 22, 2019
$(document).ready(function(){
    $('filledField input').on('change',function(){
      var value = $(this).val();
      $('.radio').find($('input[value="' + value + '"]')).prop('checked', true);
    });
});

 

0 0
replied on July 22, 2019

Hi Jonathan,

Have you tested this with a submission? The reason I asked is that sometimes JavaScript changes aren't saved when the form is submitted if you don't trigger the change event (i.e, adding .change() at the end after setting the prop value).

1 0
replied on July 23, 2019

Hi Jason,

I just checked the saved form in the repository and it does have the radio button still checked.

Thanks :)

0 0
replied on October 7, 2021 Show version history

Just to note, it seems the lookup events gets kicked off for EACH lookup that you specified in your forms.  So it may get kicked off more than once which doesn't seem ideal.

0 0
replied on October 7, 2021

@████████ That's correct, the lookupstarted and lookupcomplete events fire for every lookup that is executed on the form.

However, the event parameter will allow you to filter and target specific lookups so you can dictate which code fires for specific lookups.

For example, if the field that triggers your desired lookup has the id Field1, you could use the following inside your lookup handler.

if (event.triggerId == "Field1") {
    myFunction();
}

That way, even though the lookupstarted or lookupcomplete event fires for every lookup, your code will only execute for a specific lookup.

You can also do this with the lookup rule Ids, but that gets a little more complicated because rule id values change when you make changes to the lookups.

Can we get a "lookups complete" custom event listener please? - Laserfiche Answers

 

1 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.