views:

22

answers:

1

I need to pass a value to javascript from different form fields when the user hits enter, i.e.:

a=form1.value
b=form2.value

etc.

How can I do this?

A: 

That depends on the type of form fields, but for most fields this will work:

var a = document.getElementById('id_of_form1').value;
var b = document.getElementById('id_of_form2').value;

You could also consider using a framework such as jQuery or Prototype that would make it a lot easier.

To do this when the user presses enter, set up an event handler on whichever element it is the uses presses enter on. Again, methods varies depending on element and browser. Using a framework is recommended.

Example for jQuery:

$('#foo').bind('keypress', function(event) {
    if (event.which === 13) {
        var a = $('#id_of_form1').val();
        var b = $('#id_of_form2').val();
        // use a and b here
    }
});

Where foo is the id of the element on which you want to listen for enter.

Jakob Kruse
I am using jQuery on the site already, so a solution with jQuery is not a problem
Constructor