Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » Need help working with forms and Javascript

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

Page: 1
Go to End
Author Topic: 498 Views
  • helenclarko
    Avatar
    Community Member
    25 Posts

    Need help working with forms and Javascript Link to this post

    Hi all,

    I have been trying to get my head around this for awhile now, but cant seem to work out this last little bit.

    Let me explain what I'm doing.

    I have an events page where someone can enrol for an event, when they click "Enrol" it takes them to a form which has been populated with things like the date, location, etc for the event. They can then enter missing details like their names, email address, etc.

    I am using the following to achieve the above:

    //Function for adding subject to subject box
       if($('#Form_Form').length > 0){
          var course = decodeURIComponent($.getUrlVar('cour'));
          var date = decodeURIComponent($.getUrlVar('date'));
          var time = decodeURIComponent($.getUrlVar('time'));
          var loc = decodeURIComponent($.getUrlVar('loc'));
          var price = decodeURIComponent($.getUrlVar('price'));
          if(course != 'undefined'){
             $('#EditableRadioField39').hide();
             $('#Form_Form_EditableTextField40').val(course);
          } else {
             $('#EditableTextField40').hide();
          }
          if(date != 'undefined'){
             $('#Form_Form_EditableDateField13').val(date);
          }
          if(time != 'undefined'){
             $('#Form_Form_EditableTextField12').val(time);
          }
          if(loc != 'undefined'){
             $('#Form_Form_EditableTextField14').val(loc);
          }
          if(price != 'undefined'){
             $('#Form_Form_EditableTextField36').val(price);
          }
       }

    The only issue I have is that I need to make something that changes the price dependent on how many people sign up for the event.

    I cant seem to create a new function which works.

    how can I work out if a EditableTextField has nothing assigned to it?

    The following is what I am trying:

    if($('#EditableTextField21').var != ''){
       $('#Form_Form_EditableTextField36').val(price*2);
    }

    EditableTextField21 is a text field for the persons Name.
    EditableTextField36 is a field for the price.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Need help working with forms and Javascript Link to this post

    $('#EditableTextField21').var should be $("#EditableTextField21").val();

  • helenclarko
    Avatar
    Community Member
    25 Posts

    Re: Need help working with forms and Javascript Link to this post

    you're right, my mistake.

  • helenclarko
    Avatar
    Community Member
    25 Posts

    Re: Need help working with forms and Javascript Link to this post

    Hi Willr,

    It works!

    However it doesn't automatically update when something is entered into "#EditableTextField21".
    Am I expecting too much from javascript?

    Thanks
    -helenclarko

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Need help working with forms and Javascript Link to this post

    However it doesn't automatically update when something is entered into "#EditableTextField21".

    Make sure you function is included within a change event that is fired when you edit the field.

    $("#EditableTextField21").change(function() {
    // do something
    });

    Lot's of docs on the web for how to use events in javascript - http://www.codecademy.com/courses/jquery-events/1#!/exercises/0

    498 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.