James123
James123

Reputation: 11652

div id contains length is zero

I am checking Div id contains. If that Div exists show show another div to show or hide.

If I use full div id length is showing 1 else always zero.

$(function () {
  alert($('div[id="dnn_ctr2555_DNNWebControlContainer_ctl01_SPE_MBRDemographicsControl_pnlDemographics"]').length);
  if ($('div[id^="_SPE_MBRDemographicsControl_pnlDemographics"]').length) {
    /* it exists */
    $('#DemographicCntrlContent').show();
  }
  else {
    /* it doesn't exist */
    $('#DemographicCntrlContent').hide();
  }
})

what wrong in my if condition.

Upvotes: 0

Views: 166

Answers (2)

Jaromanda X
Jaromanda X

Reputation: 1

[attr^=abc] checks for attribute that begins with abc

[attr$=abc] checks for attribute that ends with abc

[attr*=abc] checks for attribute that contains the string abc

[attr~=abc] checks for attribute that is equal to abc - so hello abcd wont match, but hello abc will

[attr|=abc] checks for attribute that is abc or abc-... i.e. the exact word abc or abc followed by - (then followed by anything)

Upvotes: 4

DinoMyte
DinoMyte

Reputation: 8868

You are using a carat(^) symbol which basically means 'starts with'. If you wish to look for contains , you can use div[id*=.

if ($('div[id*=""]').length)

Another way of checking this is through .contains() selector:

if($("div").attr("id").contains('_SPE_MBRDemographicsControl_pnlDemographics')) // return true - false

Upvotes: 1

Related Questions