Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Need help working with forms and Javascript


Go to End


5 Posts   1231 Views

Avatar
helenclarko

Community Member, 166 Posts

7 December 2012 at 2:12pm

Edited: 07/12/2012 2:24pm

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.

Avatar
Willr

Forum Moderator, 5523 Posts

8 December 2012 at 4:37pm

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

Avatar
helenclarko

Community Member, 166 Posts

12 December 2012 at 10:51am

Edited: 12/12/2012 11:01am

you're right, my mistake.

Avatar
helenclarko

Community Member, 166 Posts

12 December 2012 at 11:05am

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

Avatar
Willr

Forum Moderator, 5523 Posts

13 December 2012 at 7:58pm

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