0

I want to get value of input fields with dynamic id to jquery function

<input type="text" id="a_8" name="a_8" value="12">
<input type="text" id="b_8" name="b_8" value="22">
<button type="button" class="btn btn-success" onclick="javascript:det(8);">Submit</button>

<input type="text" id="a_9" name="a_9" value="22">
<input type="text" id="b_9" name="b_9" value="52">
<button type="button" class="btn btn-success" onclick="javascript:det(9);">Submit</button>

Jquery function

function det(a)
{
//how can I get values of id a_8,b_8  when first button is clicked
and a_9,b_9 values when second button is clicked
}

2 Answers 2

1

you can simply create id of your control by appending method argument a to a_ or b_ like $("#a_"+a).val()

function det(a)
{
  alert($("#a_"+ a ).val());
  alert($("#b_"+ a ).val());
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="a_8" name="a_8" value="12">
<input type="text" id="b_8" name="b_8" value="22">
<button type="button" class="btn btn-success" onclick="javascript:det(8);">Submit</button>

<input type="text" id="a_9" name="a_9" value="22">
<input type="text" id="b_9" name="b_9" value="52">
<button type="button" class="btn btn-success" onclick="javascript:det(9);">Submit</button>

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

Comments

0

    function det(a){
        var b1 = $('#a_'+a).val();
        var b2 = $('#b_'+a).val();
        console.log('values: '+b1+','+b2);
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="a_8" name="a_8" value="12">
<input type="text" id="b_8" name="b_8" value="22">
<button type="button" class="btn btn-success" onclick="javascript:det(8);">Submit</button>

<input type="text" id="a_9" name="a_9" value="22">
<input type="text" id="b_9" name="b_9" value="52">
<button type="button" class="btn btn-success" onclick="javascript:det(9);">Submit</button>

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.