HTML:
<div class="pocSection"> <div id="poc1" class="poc"> <div class="subheader"><h4 class="secTitle">Primary POC</h4></div> <div class="innerField"> <span class="lname error"><input type="text" size="15" maxlength="15" name="Last_Name" class="Last_Name" /></span> <span class="fname error"><input type="text" size="15" maxlength="15" name="First_Name" class="First_Name" /></span> <span class="mi error"><input type="text" size="15" maxlength="15" name="Middle Initial" class="Middle_Initial" /></span> </div> <input type="button" class="addPoc" value="Add Another POC" /> </div> </div>
jQuery:
$('.addPoc').live('click', function(event){
var pocNum = $('.pocSection > div').length;
if (pocNum > 9){
alert("You have reached the maximum of 9 additional POC's");
}else{
var newPoc = $("#poc1").clone().attr('id', 'poc' + (pocNum + 1));
newPoc.find('.addDeleteAnotherPoc').append('<input type="button" class="deletePoc" value="Delete POC" />');
newPoc.find('.subheader').html('<h3>Additional POC</h3>');
newPoc.find('input[type="text"]'.val("");
newPoc.appendTo('.pocSection');
}
return false;
});
As you can see, when the end user clicks the button with the class of 'addPoc', the div with an id of 'poc1' is cloned, and added to the bottom of the 'pocSection' div. Next I have a 'keyup' function that changes the style of the span tag that wraps the inputs of the form. The default class of error is simple padding on the span tag. The class of check adds a background color to that padding, giving the appearance of a colored box wrapping the input field.
$('.Last_Name').keyup(function(){
var lnameLen = $(this).val().length;
if(lnameLen > 2){
if($('.lname').hasClass('error')){
$('.lname').removeClass('error').addClass('check');
}
}
});
Ok, so here's the issue. When I clone the form, there are two input tags with class="Last_Name", so when I type in the first one, it also targets the second one. When the code satisfies the condition of var lnameLen being greater than 2, both input fields change their class. How do I differentiate between the two? I am assuming something like the following:
$('#poc1 .Last_Name').keyup(function(){...
$('#poc2 .Last_Name').keyup(function(){...
Not sure though. Any assistance will be greatly appreciated.
Thanks!

New Topic/Question
Reply



MultiQuote



|