2

I have a code piece that generates id dynamically as checc1, checc2 etc for each dynamically created textboxes respectively. How can I get these values to the variable boxc one by one?

 function GetDynamicTextBox(value) {
  var nextRowID = 0;
  var id = ++nextRowID;
  return value+' : '+'<input class="dynfield" name = "'+value+'" id="checc' + id + '" type="text" value = "" />' + '<input type="hidden" name="fieldnames[]" value="'+value+'"/>' + '<br>' + '<br>' 
}

This is my code right now and it is wrong. This is the code in which I need the values of checc for performing validation to those dynamically created text boxes. Should I use a loop?

function boxCheck() {        

  var boxc = $('#checc').val();

  if (boxc.length == 0) {
    $('#p9').text("* Cannot be blank *");
    $("#checc").parents(".chenn").addClass("error");
    return false;
  } else {
    $("#checc").parents(".chenn").removeClass("error");
    return true;
  }
}
2
  • Did you try var id = 1; var boxc = $('#checc' + id).val(); ? Commented Mar 24, 2018 at 6:11
  • works for only one dynamically created textbox. Validation not getting applied from 2nd textbox on wards Commented Mar 24, 2018 at 6:17

1 Answer 1

1

You need to loop through the inputs using the class of inputs.

$('.dynfield').each(function() {
    var value = $(this).val(); //append them to var boxc or do the processing you want
});

To check if all of your inputs have some value

function boxCheck() {
    $('.dynfield').each(function() {
        if ($(this).val() != '') { 
            //Errors.push("Please select a vehicle"); 
            $('#p9').text("* Cannot be blank *"); 
            $(this).parents(".chenn").addClass("error"); 
            return false; 
        } else {
            $(this).parents(".chenn").removeClass("error"); 
            return true; 
        } 
    }); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

function boxCheck() { $('.dynfield').each(function() { var value = $(this).val(); if (value.length == 0) { //Errors.push("Please select a vehicle"); $('#p9').text("* Cannot be blank *"); $("#checc").parents(".chenn").addClass("error"); return false; } else { $("#checc").parents(".chenn").removeClass("error"); return true; } }); } like this?
What exactly are you trying to do? check if atleast one input has value? or check if all inputs have value?
if all inputs have values...if i have 10 input boxes, all 10 should have values....or else display error...
Updated answer @serializer

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.