views:

41

answers:

3

In the below code how to validate for null values for only the dynamically added text box i.e,r_score1,r_score2.........

  <script>
   function validate()
   {
      //How to validate the r_Score.. textfields
   }
  $(document).ready(function() {
   for(var i=0;i< sen.length;i++)
   {
     row += '<input type="text" name="r_score'+i+'" id="r_score'+r_count+'" size="8" />';
    }
   $('#details').append(row);
   });
   </script>
   <div id="details"><input type="text" name="score' id="score" size="8" /></div>
  <input type="button" value="Save" id="print" onclick="javascript:validate();" />
+1  A: 

Give the text boxes a class name and use class selector

function validate()
   {
      var boxes = $("#details input:text.classname");
      boxes.each(function(){
          var currentVal = $(this).val();
          // do your validation here
      });
   }
rahul
Rahul let me ask u this,when is id to be used and when is class to be used?
Hulk
Since your ids are generated dynamically you have to use attribute comparison operator to get all the elements with the particular pattern. So in this case I think it would be better to use a class so that you can identify the elements.
rahul
If you know the exact id then use id selector, since it is the fastest one. Otherwise use a combination of tag and class selectors.
rahul
Thanks................
Hulk
A: 
$("#details input[name^='r_score']").each(function(nr){

    if($(this).val() === "") alert("Field "+(nr+1)+" is empty");

}); 
Vincent
A: 

Use live event handler for dynamically added content.

Prasanna