curious_coder
curious_coder

Reputation: 2468

Pass two values of input fields to script and combine for searching

In my current project, i have two text fields in which user will input requirement and area respectively. Grabbing those values, i have to do an ajax call and fetch the necessary data from the database and return it. The ajax call has to be made as and when the input exceeds 4 characters in any of the text fields. As long as there was only one text field i didn't have any problem.

<input type="text" value="requirement" onkeyup="function1(this.value)" />
<input type="text" value="area" onkeyup="function2(this.value)" />

<script>
function function1(valuetosearch)
{
 //make the ajax call
}
</script>

<script>
function function2(valuetosearch2)
{
 //make the ajax call
}
</script>

How can i combine the two scripts and pass the data in ajax as an array?
P.S The main reason for scripting is to do a search combining the two input fields. For example if someone enters house, vehicle in requirement field and place1,place2 in area field. The result search should display the result for the following
1) place1 house
2) place1 vehicle
3) place2 house
4) place2 vehicle

Upvotes: 4

Views: 3945

Answers (3)

user2264587
user2264587

Reputation: 474

http://jsfiddle.net/JMpgU/

$("input").keyup( function () {
    var str = $(this).val() + " " + $(this).siblings().val();
    alert(str);
   //make the ajax call
});

with any number of inputs: http://jsfiddle.net/JMpgU/2/

$("input").keyup( function () {
    var strings = $(this).
                  siblings().
                  addBack().
                  map(function () {
                      return $(this).
                             val();
                  }).
                  toArray();
 //make the ajax call
});

Upvotes: 2

Amit
Amit

Reputation: 15387

Try this

<input type="text" value="requirement" onkeyup="functionABC()" id="First" />
<input type="text" value="area" onkeyup="functionABC()" id="Second" />

<script>
function functionABC()
{
   var searchString=$("#First").val()+" "+$("#Second").val();
   //make the ajax call
}
</script>

Pass "searchString" as a param.

Upvotes: 1

Amit Sharma
Amit Sharma

Reputation: 1212

you can set id for each elemnt and get the other's value using

document.getElementById("id1").value

and then make the ajax request

Upvotes: 3

Related Questions