0

I have the following...

 $(document).ready(function() {
        $('p:contains("You")').parent('span').prev('input').addClass('error');
  });

My function works fine given that it adds the class to the correct inputs but it should only add the class if the paragraph containing 'you' is display:inline.

Has anybody any idea of how I can do this?


My markup for each input is similar to this....

<li class="yourdetli">
    <label class="yourdet">House Number</label>
    <input type="text" id="ctl00_ContentPlaceHolder1_TB_HNumber2" name="ctl00$ContentPlaceHolder1$TB_HNumber2">
    <span style="color: Red; display: none;" class="errorp" id="ctl00_ContentPlaceHolder1_RequiredFieldValidator9">
        <p>You must complete this field</p>
    </span>
 </li>
4
  • 1
    Is visible specific enough? Commented Oct 24, 2011 at 15:01
  • I suspect you're traversing the DOM incorrectly. Can you provide the HTML? Commented Oct 24, 2011 at 15:02
  • It seems to be working fine, but for somereason its picking up the 'p' has 'You' in the content despite the display being set to none? Commented Oct 24, 2011 at 15:14
  • Your input is invalid. You can't put a <p> inside a <span>. Commented Oct 24, 2011 at 15:58

4 Answers 4

2

You could use filter() and check the display property:

 $(document).ready(function() {
        $('p:contains("You")').filter(function(){
                               return $(this).css('display') === 'inline';
                               })
                               .parent('span')
                               .prev('input')
                               .addClass('error');
  });

filter keeps the element if the function returns true

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

Comments

1
$('p:contains("You"):visible')

Assuming you aren't changing the display on your paragraphs anywhere in the document.

2 Comments

Visible is different from having display:inline
Yep, I'm just going off of his title.
1

You may want to try collecting all <p> tags that contain you and loop though all of them with a if statement.

If css display is inline then add error to input.

This may help

$(document).ready(function(){
  $("p:contains('you')").each(function(){
    if($(this).css('display') === 'inline'){
      $(this).parent('span').prev(':input').addClass('error');
    }
  });
});

Comments

0

:contains is case sensitive. "You" Not sure if that is the problem.

1 Comment

if (str.toLowerCase().indexOf("yes") >= 0) To also look for a sub string.

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.