0

i have a label generated like this. This one is generated from jquery validation.

 <label for="admin_email" generated="true" class="error" style="display: inline-block;">Please provide admin username</label>

I need to check if the label with for="admin_email" exists.

I know that for id, I can use something like this:-

if($('#some_id').length > 0)
{
    .......
}

How can I do the same with for="admin_email"?

3
  • maybe this works $('label[for="admin_email"]').length Commented Mar 21, 2017 at 12:22
  • $('label[for="admin_email"]').length Commented Mar 21, 2017 at 12:22
  • $("[for='admin_email']") should work. See jquery attribute selector Commented Mar 21, 2017 at 12:22

2 Answers 2

1

Try this:

if ($('label[for="admin_email"]').length > 0) {
  // code goes here
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use css selectors, so for example label[for="admin_email"]

if($('label[for="admin_email"]').length === 1) {
    console.log('for="admin_email" exists');
} else {
    console.log('for="admin_email" does not exist');
}

if($('label[for="admin_email_label_that_doesnt_exist"]').length === 1) {
    console.log('for="admin_email_label_that_doesnt_exist" exists');
} else {
    console.log('for="admin_email_label_that_doesnt_exist" does not exist');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="admin_email" generated="true" class="error" style="display: inline-block;">Please provide admin username</label>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.