0

I have been able to hide/show form controls using JavaScript. Is there a way to capture jQuery events on these controls? For instance, I want an onclick function to work on my form control. How would I go about doing something like this? Is there a similar function like the _spBodyOnLoadFunctionNames where I will be able to trigger events on specific form controls?

1 Answer 1

1

To use jQuery you will need to know the control ID in question so you can add your own handlers.

The simplest way I've found to do this is to examine the DOM of the page in question using the debug console of your browser (F12) and then using the inspector tool to select the control and find the id.

You could then have your jQuery document.ready function associate your own event handlers to the controls in question.

Here is simple example:

$(document).ready(function(){ $('#button').on('click', function(){ //do whatever should happen on click

}); });

Where #button is the control ID of a Sharepoint control (I.e. A button, etc.).

5
  • I have tried does not seem to work. $('.ms-formbody:input[id $=TextField]').click(function() { alert("clicked")});
    – lazoDev
    May 12, 2014 at 18:10
  • Just to eliminate the obvious go ahead and add the selector in using just the ugly looking id of one of your controls. Also make sure this code runs after the document has loaded (using the document.ready function) by putting an alert in that function (just to make sure document.ready is getting called and not erroring due to some silly javascript error) May 12, 2014 at 18:17
  • I got it! Looking for wrong attribute type worked with title! $(document).ready(function() { $("input[title *='BEMS ID']").click(function() { alert("clicked")}); });
    – lazoDev
    May 12, 2014 at 18:19
  • 1
    Yay! Good job. Now if they could all be this quick :) May 12, 2014 at 18:20
  • Probably will work like you suggested too. I think I might have had a syntex error. Thanks for the help a bunch! Have an upvote and a solved :)
    – lazoDev
    May 12, 2014 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.