1

I have input form where I have error text in the array and I loop through it to assign input a error text fields, but my issue is that if the error for example on the last input it displays the first array option value not the last as should be. code below: have no access to the html.

If for example first fiel whats your name filled and there i no error message than the "What's your name?" is displayed on where do you work input and not the "Who do you work for?" errro message, if all inputs are empty it works fine

var arr = [ "What's your name?", "Who do you work for?", "Most convenient 
phone number to call you on:", "Best email address to contact you on:", 
"Tell us what you'd like to know about:"];

    // setting the interval to check that the elements displayed as needed
    var timer = setInterval(function () {
        for (var i=0, l=arr.length; i<l; i++) {             
            // i believe the below line has the issue  
            (jQuery('.validation_message:eq('+[i]+')').text(arr[i])); 

        }    
    }, 500)
10
  • Can you post a working example using the snippet tool? (Ctrl+M) Commented Jul 16, 2017 at 15:07
  • also please share the html Commented Jul 16, 2017 at 15:07
  • 1
    Remove the square brackets from i in the :eq part of the selector. Commented Jul 16, 2017 at 15:10
  • 2
    How about you try a couple of complete sentences? Commented Jul 16, 2017 at 15:12
  • 1
    It does return the right value, it's just that your logic is wrong because every 500ms you're looping through the entire list again Commented Jul 16, 2017 at 15:14

2 Answers 2

2

If you feel the issue is (probably) with that line, how about taking a reference to the jQuery dom object targeting .validation_message class and then use a eq function?

   var timer = setInterval(function () {
        var validationMsgDom = jQuery('.validation_message');
        for (var i=0, l=arr.length; i<l; i++) {             
            validationMsgDom.eq(i).text(arr[i]); 
        }    
   }, 500)
Sign up to request clarification or add additional context in comments.

Comments

1

sorry was my mistake from my side, when selecting by eq(), if the the error does not appear the div is not created, so what I did added the parent div id than it all worked code below:

            jQuery('.gfield_contains_required').each(function(idx) {
            jQuery(this).attr('id', 'a' + idx);
            });

            (jQuery('#a' + i + ' .validation_message').text(arr[i]));

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.