Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
How to get html element tag/text when clicked in Jquery?

I have a context menu where I want to use certain method it finds p/div, so right now I'm just logging so that to see if I find the proper element when I click.

HTML

<div>Hello I'm div</div>

<p>I'm para <span>I'm span inside para</span></p>

code

    var paraElm = $target.closest("p")
       if(paraElm){
          console.log("i have selected the element is para",paraElm)
         }

it gives me an array in output

log for para

i have selected the element is para,
n.fn.init [p.locked.sender, prevObject: n.fn.init(1),context: p.locked.sender]
0: p.locked.sender
context: p.locked.sender
length: 1

log for span inside para

i have selected the element is para,
n.fn.init [p.locked.sender, prevObject: n.fn.init(1), context: span.selected]
0: p.locked.sender
context: span.selected
length: 1

when I clicked on span, it shouldn't log even if it's inside p tag

UPDATE#1: So I made some changes, used a condition to remove won’t log for span tag, just in JSFiddle it's not working as intended, but works as i expected it to.

$(document).ready(function() {
  $("body").on('click', function(event) {
    var $target = $(event.target);

    var getElm = $target.closest('p');

    if (getElm.context.nodeName.toLowerCase() != 'span') {
      console.log("I got the element", getElm);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Hello I'm div</div>

<p>I'm para <span>I'm span inside para</span></p>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

// i hope this solve your issue
$(document).ready(function() {
  $('span').addClass('span');
  $('body').on('click', function(event) {
      editTask(event.target);
  });
});

function editTask(node) {

    if(!($(node).hasClass('span'))){
  var closest_element = $(node).text();
  console.log(closest_element);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Hello I'm div</div>

<p>I'm para <span>I'm span inside para</span></p>

about 4 years ago · Juan Pablo Isaza Relatório

0

I made some changes, used a condition to remove won’t log for span tag, but works as I expected it to.

$(document).ready(function() {
  $("body").on('click', function(event) {
    var $target = $(event.target);

    var getElm = $target.closest('p,div');

    if (getElm && getElm.length) {
      console.log("I got the element", getElm[0]);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Hello I'm div</div>

<p>I'm para <span>I'm span inside para</span></p>

   <p>I'm para 2</p>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda