0

I have this HTML or mark up .

<fieldset class="ui-dform-fieldset">
    <input type="text" id="totalRetryCount" name="totalRetryCount" tabindex="1" onblur="validateElement('Configuration', 'testSuiteConfigurationform','totalRetryCount')" class="ui-dform-text">
    <legend class="ui-dform-legend">Total Retry Count</legend>
    <label for="totalRetryCount" class="error">Please enter a valid number.</label>
</fieldset>

I want to check whether label have error class or not.I did like that

when I debug my element value is "totalRetryCount" .But it is not showing alert.

element ="totalRetryCount"

if($("#"+element+" "+"label").hasClass('error')){
        alert('-sdafs-')
    }
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
Shruti
  • 1,554
  • 8
  • 29
  • 66

7 Answers7

5

Your selector is wrong. The totalRetryCount ID is on an input, which can have no children.

The targeted label is a sibling, so use the next-siblings-selector instead:

if ($("#" + element + " ~ label").hasClass("error")) {
    alert("found error");
}

Or this:

if ($("#" + element + " ~ label.error").length) {
    alert("found error");
}
cookie monster
  • 10,671
  • 4
  • 31
  • 45
1

Your input tag is never closed, input is also self closing. Since you have an ID, just do:

$("#totalRetryCount").siblings("label").hasClass("error");
tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

The string totalRetryCount is a value of for attribute, not an id of parent element.

var element ="totalRetryCount"
if($("label[for='"+element+"']").hasClass('error')){
    alert('-sdafs-')
}

(or)

$("#" + element).siblings("label[for='"+element+"']").hasClass("error");
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
0

Your id/class identifiers are off, you are checking for label as a child of an element with id totalRetryCount, but it is not a child of that element.

This works

HTML:

<fieldset class="ui-dform-fieldset">
    <input type="text" id="totalRetryCount" name="totalRetryCount" tabindex="1" onblur="validateElement('Configuration', 'testSuiteConfigurationform','totalRetryCount')" class="ui-dform-text">    
   <legend class="ui-dform-legend">Total Retry Count</legend>
   <label for="totalRetryCount" class="error">Please enter a valid number.</label>
</fieldset>

jQuery:

if($(".ui-dform-fieldset label").hasClass('error')){
        alert('-sdafs-')
    }

JSFiddle Demo

Dan
  • 9,391
  • 5
  • 41
  • 73
  • 1
    Personally I'm fond of `$('#' + element + ' label.error').size()`. Fewer trips through the DOM and doesn't involve another check of a property. – Brad Christie Jun 11 '14 at 14:14
  • @BradChristie this will not works, see answer http://stackoverflow.com/a/24165166/133408 –  Jun 11 '14 at 14:15
  • @PeterM: Just accidentally omitted `~` in comment. Should be `$('#' + element + ' ~label.error').size()` Could also be `$('label[for="' + element + '"].error').size()` to be more concise. – Brad Christie Jun 11 '14 at 14:21
0

Your selector is incorrect. See here for a question that explains the label selector. jQuery selector for the label of a checkbox

It should look more like this.

if ( $("label[for='" + element + "']").hasClass( 'error' ) )
{
    alert( 'Heres my alert!' );
}
Community
  • 1
  • 1
PugsOverDrugs
  • 515
  • 1
  • 4
  • 14
0
if($("#totalRetryCount").siblings('label').hasClass('error')){
    alert('-sdafs-'); }

js fiddle

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
rrapuya
  • 298
  • 2
  • 10
0
$("label[for=totalRetryCount]").hasClass('error')){
   alert('-sdafs-')
}

Actually if you don't have non alphanumeric characters in your id then you don't need to surround with ' ' single quote.

Md Nazmoon Noor
  • 3,187
  • 1
  • 24
  • 30