1

I have 5 input box, and each input box Have a different value .. how i can merge the value of the input boxes and compare it, then alert or do something ..

Example:

<form>
            <input id="input1" type="text">
            <input id ="input2" type = "text"/>
            <input id ="input3" type = "text"/>
            <input id = "input4" type = "text"/>
            <input id = "input5" type = "text"/>
</form>

    var input1 = document.getElementById('input1').value;
    var input2 = document.getElementById('input2').value;
    var input3 = document.getElementById('input3').value;
    var input4 = document.getElementById('input4').value;
    var input5 = document.getElementById('input5').value;
    var wordsConcat = input1.concat(input2,input3,input4,input5);

//after merging Compare the words to SAMPLE;

2
  • loop them and read the values...What have you tried. What do you mean by merge? Commented Feb 10, 2014 at 13:30
  • compare in the sense if present in wordsconcat or the entire concatinated word is sample??? Commented Feb 10, 2014 at 13:32

5 Answers 5

9

You've tagged jQuery so I'm assuming it's available to you.

You can map each input to a value, and then join() them together using an empty string:

var vals = $('input').map(function(){
    return this.value;
}).get().join('');

JSFiddle

ES6:

const vals = $('input').map((_, elem) => elem.value).get().join('');
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it using each() method in jQuery

var a='';
$('input[type=text]').each(function(){
   a+=this.value;
});
if (a === "SAMPLE") {
     //do something
}

Fiddle Demo

Comments

0
var sample = 'your word';
if(wordsConcat === sample){
   alert('a');
}

Comments

0

You can try this.

var inputs = document.getElementsByTagName("input"),
    inputValue = "";
    for (var index=0; index < inputs.length; index ++ ) {
        inputValue += inputs[index].value;
    }
    if (inputValue === "SAMPLE") {
         //do something
    }

Comments

0
var result = $('input[type="text"]').map(function(i, e){
    return e.value;
}).get();

//do compare stuff on result

Comments

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.