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

Question

Question

Hiding add button

asked on May 5, 2016 Show version history

Hi,

I have a form which is retrieving data from a database via lookup. The data retrieved is based on four fields that and thus there is an autofill button. There is a fifth "Action" field which is a drop down field with three options, "Add", "Delete", "Change". When I fill all the fields and hit the lookup button, data fills the table alright. But I require that when the choice in the Action field is not Add, the Add button of the Table be hidden once the autofill button is clicked. The Add button should be available only when "Add" is selected. The code am trying is not working. Please let me know what am missing. #q62 is the table add button id. action is the class of the dropdown field

$('.autofill').on('click', function () {
var actonform =  $('.action select').val();
if (actonform == "Add") {
$('#q62').show();
} else { $('#q62').hide();
}
});

0 0

Replies

replied on May 5, 2016 Show version history

Hi Mark,

I think you need to wrap the entire code you have inside a $(document).ready(...) function. The following should work:

$(document).ready(function() {
 // $('#q62').hide();
 $('.autofill').on('click',function() {
  var actonform = $('.action select').val();
  if (actonform == "Add") {
   $('#q62').show();
  } else {
   $('#q62').hide();
  }
 });
});

If you want the "Add" button hidden at the beginning you can uncomment the second line in the above snippet to hide it by default.

Hope this helps!

EDIT: changed the class name in the code to reflect OP

1 0
replied on May 6, 2016 Show version history

Hi James, 

Thanks for your quick response.  I have other code running together with this particular code and they are all wrapped up inside the $(document).ready(...) function.  Do you mean that I wrap it up again in its own function?  Thanks

Mark

0 0
replied on May 6, 2016

Hi Mark,

No that shouldn't be necessary. Funny. I can actually get it to work (in a similar environment with everything appropriately named) just by pasting your original code inside that wrapper.

You can try adding alert functions to the code to troubleshoot. For instance adding "alert(actonform);" after defining the variable actonform can test 2 things:

  1. When clicking on the Autofill button, you should get a popup message. If you don't get a popup message, then the click event isn't registering for that button. You might double-check that the class name is entered correctly for the field. Check the casing of the letters, too; JavaScript is case-sensitive.
  2. If you do get a popup, what you see is the value of the "Action" field. Check whether the values returned in this popup match what you expect for the appropriate value in the field. Again, check the casing: if the popup displays "add" instead of "Add", the script won't recognize it.

If those tests work as expected (or even if not, I suppose), consider commenting out the rest of the code and leaving the pieces relevant to this problem to test and see if something in another part of the script is interfering with this snippet.

Hope this helps.

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

Sign in to reply to this post.