0
<input class="lf" id="r3-1" type="text"/> 
<input class="rf" id="r4-1" type="text"/> <br>

<input class="lf" id="r3-2" type="text"/> 
<input class="rf" id="r4-2" type="text"/> <br>
...

How to add values from this inputs into var code=''; like this?

ABC "value r3-1" "value r4-1"

ABC "value r3-2" "value r4-2"

3
  • did u mean that the result u want may be grouped ? Commented Aug 26, 2011 at 5:57
  • yes! group like in a question Commented Aug 26, 2011 at 6:01
  • if ur group condition is the number in input's id ,like r3-1 means its group is 1, u may take @Naga Harish Movva 's answer~ Commented Aug 26, 2011 at 6:23

6 Answers 6

6

Check with this below code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>        
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<title>Getting values ​​from two different Inputs</title>
</head>
<body>
<div id="inputs">
<input class="lf" id="r3-1" type="text" value="2"/> 
<input class="rf" id="r4-1" type="text" value="3"/> <br/>

<input class="lf" id="r3-2" type="text"/> 
<input class="rf" id="r4-2" type="text"/> <br/>

<input class="lf" id="r3-3" type="text"/> 
<input class="rf" id="r4-3" type="text"/> <br/>


<input class="lf" id="r3-4" type="text"/> 
<input class="rf" id="r4-4" type="text"/> <br/>
</div>
<input id="submit" type="button" value="Test" />
<div id="OutPut">
</div>

<script type="text/javascript">

    jQuery("#submit").click(function() {
    var temp, output="";
    jQuery('#inputs .lf').each(function(index) {
    /*we can also use .rf or br
    //jQuery('#inputs .rf').each(function(index) {
    //jQuery('#inputs br').each(function(index) {
    */
            temp = "ABC ";
            jQuery('#inputs input[id$=-' + (index + 1) + ']').each(function() { temp += '"' + this.value + '" '; })            
            output += temp + "<br/>";
        });
        jQuery("#OutPut").html(output);
    });

</script>
</body>
</html>

Output:-

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You can access the value-property of your inputs like this:

$('#r3-1').val()

Then it's just a matter of string-formatting to get your var code the way you want it. I don't understand what you mean about

ABC "value r3-1" "value r4-1"

ABC "value r3-2" "value r4-2"

Comments

1

var val1 = jQuery('r3-1').val();
var val2 = jQuery('r3-2').val();
var code = val1 + val2;

Comments

1

I didnt clearly understand your question but if you want to get values of inputs u can use this.

var val1 = $('#r3-1').val()
var val2 = $('#r3-2').val()

Comments

1

You need to use JavaScript.

<script>
var code = '';
var r3_1 = document.getElementById("r3-1").value;
var r3_2 = document.getElementById("r3-2").value;

var r4_1 = document.getElementById("r4-1").value;
var r4_2 = document.getElementById("r4-2").value;

code = "ABC" + r3_1 + r4_1;
</script>

Comments

0

To get the value from a text input, the syntax is something like this:

$('#r3-1').val();

Rinse and repeat to get the other three values to build your output.

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.