Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » How to add javascript actions (like "onkeyup") to FormFields?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3243 Views
  • Tama
    Avatar
    Community Member
    130 Posts

    How to add javascript actions (like "onkeyup") to FormFields? Link to this post

    Howdy

    I've been working on a form that uses validation from our internal systems. To get it running I statically coded it and now I'd like to build it using the SS Form object.

    There's two things I'm still trying to work out how to do:

    • A few of the TextFields and NumericFields require a "onkeyup" action which trigger the validation javascript. In HTML this would look like

      onkeyup="validateValuationNumber(this.value)"

      but I'm not sure what to add to this entry

      new TextField('ValuationNumber', 'Valuation Number', '' ),


    • I want to be able to place an empty DIV between two of the formfields which I can populate with dynamic information.

    I'm guessing that these are both possible but after wading about the documentation and forums I still can't work out how I'm supposed to do it.

    Any suggestions would be greatly appreciated.

    Cheers
    Tama

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: How to add javascript actions (like "onkeyup") to FormFields? Link to this post

    The form field API doesn't support adding events. I recommend you use jQuery to add event handlers to your form eg http://api.jquery.com/keyup/

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to add javascript actions (like "onkeyup") to FormFields? Link to this post

    Thanks Will - adding this works well:

    <script name="javascript">
    $('#Form_PaymentForm_ValuationNumber').keyup(function(validateValuationNumber) {
    ...

    I also want to place an empty div between two of the formfields I can use for the custom validation. I know I can use a Read Only field to place a span but this will require DHTML after the load to make it invisible. Is there a tidier way?

    Cheers
    Tama

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: How to add javascript actions (like "onkeyup") to FormFields? Link to this post

    also want to place an empty div between two of the formfields I can use for the custom validation

    document.appendElement to add an empty div then via javascript. You could also try using the RightTitle value. I think it puts the right title in a div (which then you add another div inside of).

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to add javascript actions (like "onkeyup") to FormFields? Link to this post

    To append the a new div called "valuationValidation" after the div "ValuationNumber" I used the following code:

    jQuery('#ValuationNumber').after($('#valuationValidation').append('<div>'));

    I ended up using the "jQuery" prefix instead of "$" as their seemed to be a conflict (with Prototype?)

    3243 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.