0

I have two input field :

 <input type="text" class="form-control input-td" name="getmark[]" id="getMark_<?php echo $i++?>"  required>
 <input type="text" class="form-control input-td" name="mcq[]" id="mcq_<?php echo $i++?>" required>

I am trying :

 $('input[id^="getMark_"]','input[id^="mcq_"]').on('change', function() {  
       var get_mark = this.value;
       var mcq      = this.value;

 });

i am not sure how can call $('input[id^="getMark_"]','input[id^="mcq_")

How can call two input filed .

5
  • try with classname, this id method looks messy, $('.form-control input-td:nth-chile(1)','form-control input-td:nth-chile(1)').on('change', function() { Commented Apr 12, 2019 at 8:46
  • i have many student data . so i need check getMark & mcq input field .this student pass or fail of this subject Commented Apr 12, 2019 at 8:51
  • did you try to select element by name $('td[name=name]') Commented Apr 12, 2019 at 8:53
  • You are not closing your second input properly. You forgot a '... Try like this: $('input[id^="getMark_"]','input[id^="mcq_"').on('change', function() { Commented Apr 12, 2019 at 8:54
  • @mindmaster . you say right but code not work Commented Apr 12, 2019 at 8:57

4 Answers 4

1

You have syntax error and input is a proper event for your situation

$('input[id^="getMark_"], input[id^="mcq_"').on('input', function() {  
		var mark = mcq = '';
    if($(this).attr("id").indexOf('getMark_') !== -1){
      mark = this.value;
    }else{
    	mcq = this.value
    }
		console.log(mark,mcq);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control input-td" name="getmark[]" id="getMark_<?php echo $i++?>"  required>
 <input type="text" class="form-control input-td" name="mcq[]" id="mcq_<?php echo $i++?>" required>

It will get the change in the input textbox.

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

2 Comments

Your code work . Just change .on('input' to on('change ,
Either way worked. You can take your call as per your requirement
1

For your code that will work:

$(document).on('change','input[id^=getMark_], input[id^=mcq_]', function() {  
       var get_mark = this.value;
       var mcq = this.value;
 });

So you don't need "" or '' inside attribute selector... But you have also logical error your get_mark and mcq variable will be always the same.

So you need two separate methods to get those variables, that would be better choise

$(document).on('change','input[id^=getMark_]', function() {  
           var get_mark = this.value;
     });

$(document).on('change','input[id^=mcq_]', function() {  
           var get_mark = this.value;
     });

If you need you can save them to global variables.

3 Comments

i need check two input value tin real time . that this student pass or fail
Then you need to have some Submit button where you get and check those values at one...
i have two input field mcq & getMark i need to check every mark pass or fail . if pass or fail then result show other input field
0

You can do something like

 $(' input[name="getmark[]"], input[name="mcq[]"] ').on('change', function() {  
       var get_mark = $('input[name="getmark[]"]').val();
       var mcq      = $('input[name="mcq[]"]').val();
       
       console.log('get_mark', get_mark);
       console.log('mcq', mcq);

 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control input-td" name="getmark[]" id="getMark_<?php echo $i++?>"  required>
 <input type="text" class="form-control input-td" name="mcq[]" id="mcq_<?php echo $i++?>" required>

Comments

0

Replace your html by :

  <div id='student_<?php echo $student_id; //$i++; ?>' class='student'>
  <input type="text" class="form-control input-td" class="getmark" name="getmark[]"    id="getMark_<?php echo $i++?>"  required>
  <input type="text" class="form-control input-td"  class="mcq"  name="mcq[]" id="mcq_<?php  echo $i++?>" required>
  <input type="text" class="form-control input-td" class="is_success" readonly>
 </div>

Repace your js by :

$('.getmark, .mcq').on('change', function() {  
   var get_mark = this.value;
   var mcq      = this.value;
  if(get_mark > 18)
    $(this).find('.is_success').val()='pass';
   else 
     $(this).find('.is_success').val()='fail';
});

To collect all of them :

function getallstudentmark()
{
arr_allstudents = new Array();

  $(.student).each(function (index, value) {
    obj_student = new Object();
    obj_student.id= $(this).attr('id');
    obj_student.get_mark = $(this).find('.getmark').val();
    obj_student.mcq = $(this).find('.mcq').val();
    obj_student.success = $(this).find('.is_success').val();
    arr_allstudents.push(obj_student);
  });
return arr_allstudents;
}

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.