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

Question

Question

Javascript to make custom button change collection field in same set only

asked on May 1, 2019

Hi folks,

We have buttons in a custom html field on a form. We use these to change the selection in a drop-down field through javascript created from eloquent solutions found here previously. This has been working quite well for the individual instance of the form, but are looking to expand it into a collection with a fixed number of instances. The code, which uses classes, changes every set in the collection (understandably) but would like to restrict the actions to only impacting their respective individual sets.

Tried limiting it with a collection class eg '.Collect01' and '.rpx', then looking into event listeners but not succeeding.

Thinking it may require a function rewrite using '.each' and '.closest' perhaps?

Current single instance form references:

<p><input class="AButt" type="button" value="Item1">
<input class="BButt" type="button" value="Item2"></p>
$(document).ready(function () {  
      
  function buttStamp() 
  {      
    if ($(this).hasClass('AButt'))
    {
      $('.DropMenu select').val("Choice1").change();
    }
    if ($(this).hasClass('BButt'))
    {
      $('.DropMenu select').val("Choice2").change();
    }
  }

    $('.AButt,.BButt,.CButt').click(buttStamp);

});

So someone taps 'Item1' button (.AButt) in the html field and it changes the dropdown menu field (.DropMenu) selection to "Choice1", but we only want that for that row/set. Next row might need to be "Choice2" etc.

Would someone have a direction for me please?

0 0

Answer

SELECTED ANSWER
replied on May 2, 2019

You could try something like

$(this).parents('.rpx').find('.myclass select');

Basically you find the parent of the button, then only look for your target inside of that parent.

Another option is to extract the index of the collection item. I have a post discussing that here.

2 0

Replies

replied on May 2, 2019

Legend. That's beautiful. Looks to be working a treat.

Much appreciated.

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

Sign in to reply to this post.