Sean Anderson
Sean Anderson

Reputation: 29311

HtmlDivElement has no method 'indexOf'

I am attempting to derive some identifying property from this element:

<div class="rtsLevel rtsLevel1">
  <ul class="rtsUL">
    <li class="rtsLI rtsFirst rtsLast">
      <a class="rtsLink rtsSelected" href="#">
        <span class="rtsOut">
          <span class="rtsIn">
            <span class="rtsTxt">Dashboard</span>
          </span>
        </span>
      </a>
    </li>
  </ul>
</div>

using this javascript:

function OnClientDragging(sender, eventArgs) {
    var target = eventArgs.get_htmlElement();
    console.log(target);
    var currentZone = TryGetZoneFromTarget(target);
}

function TryGetZoneFromTarget(target) {
//If the mouse moves too quickly target.id returns a red herring.
if (/\S/.test(target.id) == false) {
    if (target.indexOf("rtsLevel") != -1) {
        return null; //The tabstrip has an invisible div which doesn't have an ID, but isn't a redherring.
    }

    return undefined;
}

The div doesn't have an id specified, but I don't seem to be able to 'talk' to it at all, either. I've tried '.hasClass("rtsLevel")' and was being told hasClass not defined, so I tried to do a string comparison, but hit the same wall.

What am I doing incorrectly?

EDIT: Solution was if ( $(target).hasClass('rtsLevel') ) {

Upvotes: 1

Views: 4548

Answers (1)

jfriend00
jfriend00

Reputation: 707556

If target is a valid DOM element, then you should be able to do:

target.className.indexOf("rtsLevel") != -1

or if you're using jQuery and target is a DOM element, you can do:

$(target).hasClass("rtsLevel")

if target.className is undefined, then target is not a DOM element and that would be your problem.

Upvotes: 4

Related Questions