Julian
Julian

Reputation: 1831

Changing value in select form element before data is submitted

Trying to change the value of select on submission. The system does not accept "$" or "," so I'm cleaning the data.

jQuery

jQuery("#vfb-53").val( jQuery("#vfb-53").val().replace(/\D/g,'') );
jQuery("#vfb-55").val( jQuery("#vfb-55").val().replace(/\D/g,'') );

Html Form

http://onetrustseniorlending.com/mortgage-loan-calculator

On submit the field goes blank and when I check the network console it's blank. How do I fix this?

Code in Full

$('.visual-form-builder').submit(function(e){

        setCookie('othla',jQuery(".age").val().replace(/\D/g,''),2);
    setCookie('othlv',jQuery(".homeValue").val(),2);
    setCookie('othld',jQuery(".mortgageDebt").val().replace(/\D/g,''),2);

        sim_form$ = $('.crm-form').parent('form');

    sim_form$.attr({'action' : '#'});
    sim_form$.find('.firstname').attr({'name' : 'firstname'});
    sim_form$.find('.lastname').attr({'name' : 'lastname'});
    sim_form$.find('.email').attr({'name' : 'email'});
    sim_form$.find('.phone').attr({'name' : 'phone'});
    sim_form$.find('#vfb-53').attr({'name' : 'cf_1214'});
    sim_form$.find('#vfb-55').attr({'name' : 'cf_1212'});


    sim_form$.prepend('<input type="hidden" name="apipassword" value="0n3trustAPI">');
    sim_form$.prepend('<input type="hidden" name="apimethod" value="create">');
    sim_form$.prepend('<input type="hidden" name="apimodule" value="Leads">');
    sim_form$.prepend('<input type="hidden" name="assigned_user_id" value="2">');
    sim_form$.prepend('<input type="hidden" name="_redirect_url" value="' + document.URL + '#loan-schedule-container' + '">');

    jQuery("#vfb-53").val( jQuery("#vfb-53").val().replace(/\D/g,'') );
    jQuery("#vfb-55").val( jQuery("#vfb-55").val().replace(/\D/g,'') );



        //$('.loan-schedule-container').show();
        //e.preventDefault();
    });

Upvotes: 1

Views: 289

Answers (2)

Entoarox
Entoarox

Reputation: 703

You are trying to change the value of a <select> element, but select elements only allow for the options that are predefined be chosen as their value, so by trying to change its value its selecting a non-existant <option> This could be solved by replacing the select element with an input element.

Another solution is to have the visible select be used only for user-input, but having a hidden input contain the 'clean' value that you use in your code.

Upvotes: 1

tjb1982
tjb1982

Reputation: 2259

I think the issue is that trying to set the value of the select to a value that isn't in the set of options will result in undefined or an empty string.

Upvotes: 0

Related Questions