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

Question

Question

Javascript to change a B to the word Both in a Collection

asked on October 6, 2017

I was trying to use some JavaScript like this...

$(document).ready(function() {
  $('.resides').on('change', 'input', changeWord);
  function changeWord () {
    if ($(this).val() == 'B') {
      $(this).val('Both');
    }
    if ($(this).val() == 'S') {
       $(this).val('Shared');
    }
  }
}); 

to change the letter B to the word Both and the Letter S to the word Shared etc.  This doesn't work though. What have I missed?

The fields are in a Collection

I had this JavaScript (from a previous question)..

https://answers.laserfiche.com/questions/128190/Changing-a-field-value-from-B-to-Both

$(document).ready(function() {
   $(document).on('lookupcomplete', function(event){
    	if ($('.resides input').val() == 'B') {
			$('.resides input').val('Both');
		}
		else {
			$('.resides input').val('None');
		} 
    });
});

but this only changed the values in the first instance of the Collection.

Any help will be greatly appreciated.

 

Jonathan

0 0

Answer

SELECTED ANSWER
replied on October 6, 2017 Show version history

It's in a collection, so you should trigger the function off a change in the collection. Try this:

$(document).ready(function () {
  $('.collection').on('change', 'input', changeWord);
  function changeWord () {
    if ($(this).val() == 'B') {
      $(this).val('Both');
    }
    if ($(this).val() == 'S') {
       $(this).val('Shared');
    }
  }  
});

This same concept was addressed in your previous post. This JavaScript runs as soon as the document loads. When it loads, if you were to target the field in the collection, and not the collection itself, this line:

  $('.resides').on('change', 'input', changeWord);

would only bind to the one, single field that it sees. That's why it only changes the value of the field that's already present on document load. i.e.:

2 0
replied on November 29, 2017

@████████,

How would I change your JavaScript to also change the values on the second form? The form that is sent to the approver after the user has submitted the original form.

Change the value from B to Both when B is already on the form on loading.

Hope that makes sense?

Jonathan

0 0

Replies

replied on October 6, 2017

I've done something similar using the "each" function, but Tri Pham's solution is obviously much better.

UNTESTED!!!

$(document).ready(function() {

$(document).on('lookupcomplete', function(event){

function changeWord () {

      $(".resides input").each(function() {

          if ($(this).val() == 'B') {
            $(this).val('Both');
          }
          if ($(this).val() == 'S') {
             $(this).val('Shared');
          }


       });  

			}

	});

}); 

 

 

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

Sign in to reply to this post.