auto
auto

Reputation: 1153

Using javascript to take HTML form inputs

I would like to use javacript to simply look at a specific HTML form (i.e. radio button), find the user's selection and then respond in various ways.

Is there a way javascript can read user input or selected responses for a form?

Upvotes: 0

Views: 65

Answers (1)

Ahmet Can Güven
Ahmet Can Güven

Reputation: 5462

Yes there are lots of ways to do this.

You can check user input when changed. One of them is below.

 <input type="text" onchange="changed(this);"/>

This input will call changed() function with the paremetre of self. So you can control its value and do some things.

function changed(input){
  alert(input.value)  // check for input value
  if(input.value.length > 54){ //do if value is longer than 54 characters
     //Do something
  }
}

You can find lots of ways on the internet. You can try using jQuery which will be easier for you.

$("#myInput").change(function(){   //select the input box which id is 'myInput' then run function when its value changed each time
    alert("Wow, my value changed to:"+$(this).val())   //Get value with $(this).val() and alert!
});

You can also bind this to all input fields using $("input").change(function(){


Changing my answer by your request.

<form name="myForm">
   <!--Lots of divs here with input. You must hide these divs without touching form! -->
</form>

When you are on the last page all you need to do is

function listUserValues(){
   var els=document.myForm.elements;
   var l=els.length;
   for (var i=0; i<l; i++)
   {
      alert('Field Name: '+els[i].name+'\nField Type: '+els[i].type+'\nField Value: '+els[i].value);
      //Do whatever you want with each value here.
   }
}

Upvotes: 1

Related Questions