views:

40

answers:

3

How can I get the value of a form in html code to pass as a javascript argument?

Is this correct? my script takes two arguments one from textbox, one from the dropdown box.

<body>
<form name="valform" action="" method="POST">

Credit Card Validation: <input type="text" id="cctextboxid" name="cctextbox"><br/>
Card Type: <select name="cardtype" id="cardtypeid">
  <option value="visa">Visa</option>
  <option value="mastercard">MasterCard</option>
  <option value="discover">Discover</option>
  <option value="amex">Amex</option>
  <option value="diners">Diners Club</option>
</select><br/>
<input type="button" name="submit" value="Verify Credit Card" onclick="isValidCreditCard(document.getElementById('cctextboxid').value,document.getElementById('cardtypeid').value)" />
</body>
A: 

javascript argument means javascript function argument? If Yes:

You can write some function. Which serialize your form in object like {"name":"value"} and next you can pass this object in function.

Falcon
+1  A: 

HTML:

<input type="text" name="name" id="uniqueID" value="value" />

JS:

var nameValue = document.getElementById("uniqueID").value;
Am i doing it right? Check my edit and let me knowwww
@shorty876: Did you test it yourself? o_0 That would be a pretty good way of determining whether or not you did it right.
Jeff Rupert
Yeah, but it just returns a true/false and im not sure how to determine if the function was even called. Thus you can help.
+1  A: 

document.forms will contain an array of forms on your page. You can loop through these forms to find the specific form you desire.

var form = false;
var length = document.forms.length;
for(var i = 0; i < length; i++) {
    if(form.id == "wanted_id") {
        form = document.forms[i];
    }
}

Each form as an elements array which you can then loop through to find the data that you want. You should also be able to access them by name

var wanted_value = form.someFieldName.value;
jsFunction(wanted_value);
Codeacula