How to read last element from Section in jquery

I want to check if the div having class name as "divclass" contains anchor tag, if contains I have to execute some javascript code else nothing to do.

<div class="divclass">
      <span> something text</span>
      <a href="/"> Anchor tag</a>
      <span> something text 2</span>
      <a href="/"> Anchor tag</a>
    </div>

How can we do that with jquery?

EDIT: In above HTML it has last element as anchor tag, so I want to do something code here but in below HTML I don't have anchor tag inside div so I don't want to do anything.

     <div class="divclass">
              <span> something text</span>
              <a href="/"> Anchor tag</a>
              <span> something text 2</span>
<span> something text 3</span>
            </div>

Answers


Use tagName to get name of tag. It returns name of tag in uppercase format.

Try:

$(".divclass").each(function () {
    if ($(this).find(":last").prop("tagName") == "A") {
        //do something
    }
});

DEMO here.


Need Your Help

NameError when using input() with Python 3.4

python command command-prompt nameerror

I am a new Python user and I have been working through a number of tutorials. This has included running some of the code from the Command Prompt. This worked fine when I first tested the code but ...