0

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?

1

1 Answer 1

1

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.
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! So what I am trying to do is store the values when the user hits "next page" and reveal the next hidden div. So I don't want to store it as soon as it changes, only when they hit this button. Also, I don't want values on different "pages." to overlap. Is there another function rather than "onchange"? I guess I can store the values in temporary variables until they hit next page...
When user clicked next page, top link being changed?
No, just the current div becomes hidden and the next one shows.
Thank you! This looks like it should do the trick. However the returned alert doesn't seem to record the user's value. For instance if a user checks one radio button or check box, the alert says Value: on for each choice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.