Nidheesh
Nidheesh

Reputation: 4562

Pass an extra param on form submit in javascript

I have a javascript function say newFn(). I need to check some condition with if else.

function newFn(formName)
{
 var codeParam = document.getElementById('code').value; 
 var windowForm = document.forms[formName];     
   windowForm.submit(); 
 return true;           
}

My requirement is :

if(codeParam == null) I need to pass one more parameter say codeParam=null else codeParam=codeParam on windowForm.submit();.In short in the url I need an extra param. Is this possible?

Upvotes: 1

Views: 1147

Answers (2)

Moritz Roessler
Moritz Roessler

Reputation: 8641

I don't understand exactly what you want to accomplish but

function newFn(formName) {
    var codeParam = document.getElementById('code').value;
    if (!codeParam) {
        codeParam = null;
    }
    document.forms[formName].codeParam = codeParam;
    var windowForm = document.forms[formName];     
    windowForm.submit(); 
    return true;
}

If (assuming "code" is a textfield) is empty, codeParam is set to null, if it has a value, then codeParam is set to the value. (i added the propertie to the form as well, so it would get submitted, i don't know if its what you want )

You can take a look at the output of this Fiddle

Upvotes: 1

otakustay
otakustay

Reputation: 12415

Why not directly set its value like this:

if (codeParam == null) {
    document.getElementById('code').value = 'null';
}

This will send a codeParam=null form data to your action url.

However I noticed that this code would never be executed, the value property of a input element could never be the value null, when no input is provided, the value property's value is a empty string (''), so maybe you should change your logic.

Upvotes: 3

Related Questions