1

I'm working on a .net website and I have a bit of a situation.

Say I have...

<input type="text" class="name" id="name">
<p></p>

<input type="text" class="surname" id="surname">
<p>Error!</p>

What Id like to do, using javascript, is detect that the second paragraph tag says 'Error!' and add a class to the input tag before it.

I know this seems like a bit of a strange way of working but any advice would be greatly appreciated!


Hi all, the adivce and answers i was given worked fine on a fresh page on jsfiddle only i cant seem to get them to work on my actual site.

My P and input tags are constructed like this....

<li class="yourdetli">
<label class="yourdet">Street Name</label>
<input type="text" id="ctl00_ContentPlaceHolder1_TB_SName" name="ctl00$ContentPlaceHolder1$TB_SName">
    <span style="color: Red; display: none;" class="errorp" id="ctl00_ContentPlaceHolder1_RequiredFieldValidator6">
    <p>You must complete this field</p></span>
</li>

and my JS is...

<script type="text/javascript">
    $(document).ready(function() {
        $('p:contains("You must complete this field")').prev('input').addClass('error');
    });
</script>

only for some reason it doesnt seem to add my class, can anybody see why?

2
  • How are you generating <p>Error!</p>? Can you generate it as <p class="error">Error!</p> instead? Commented Oct 24, 2011 at 13:35
  • I'd recommend adding the error css class to your validator's CssClass property so that its already rendered server side. Commented Oct 24, 2011 at 14:48

5 Answers 5

2

You can use

$('p:contains("Error")').prev('input').addClass('error');
Sign up to request clarification or add additional context in comments.

2 Comments

On my page I have 12 inputs, will this work for each of them or do I need to add anything? and if the p tag has a class would $('p.errorp:contains.... work?
@Liam, yes, jQuery will implicitly loop against each matched element. If you assign a class only to the elements that contain Error! string, you don't need to use :contains selector. Only $('p.errorp'). Otherwise, yes, you are right.
1

Maybe try

$("p:eq(1):contains('Error!')").prev('input').addClass('error');    

Comments

0

You can check the text value of the second <p> tag by using jQuery. You'll need a selector that finds it (in this example, it is the last <p> tag).

if ($("p").last().text() == "Error!") {
    $("input#surname").addClass("myClass");
    // do other stuff here
}

2 Comments

He looks for second one, not the last one
He says the second one, which is also the last one. None of us know the rest of his code, so based on his example both our solutions could work.
0

Try -

if ($("input#surname").next("p:contains('Error!')").length > 0) {
      $("input#surname").addClass('yourclass');  
} 

Comments

0

I'd recommend adding the error css class to your validator's CssClass property so that its already rendered server side. This way if you change your error message, you won't have to change any javascript to display it.

<asp:RequiredFieldValidator ID="requiredValidator" CssClass="error" ControlToValidate="..." ErrorMessage="You must complete this field"/>

Or if you're looking to style all of your validators with the same class, you can add the following script to your master page.

$(document).ready(function()
{
    if (Page_Validators != null)
    {
        for (i = 0; i < Page_Validators.length; i++)
        {
            Page_Validators[i].className = "error";
        }
    }
});

See http://www.jrummell.com/blog/index.php/2009/08/apply-the-same-css-class-to-all-validators-in-a-web-project/ for more information.

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.