How to take content from a div with the same class?

Asked

Viewed 97 times

0

I think the title is quite self-explanatory but come on, I have a return of a json that comes from an sql, which can give me the following situation

    <div class="conteudo-select" style="display: block;">
        <div class="razaoSocial">
           <a onclick="pegarValor(); return false;">Nicolas e Yuri Contábil ME</a>
        </div> 
        <div class="nomeFantasia">
            <a onclick="pegarValor(); return false;">ny contabilidade</a>
        </div> 
        <div class="razaoSocial">
            <a onclick="pegarValor(); return false;">Jennifer e Thiago Lavanderia Ltda</a>
        </div> 
        <div class="nomeFantasia">
            <a onclick="pegarValor(); return false;">jt lavanderia</a>
        </div>
</div>

I’m needing to pick up the content inside the tag a...

tried with the die = event.srcElement.innerText; but does not work in Firefox, nor putting parameter in function function pegarValor(event) {

Is there any way I can do it?

2 answers

1


Try this :

HTML :

   <div class="conteudo-select" style="display: block;">
    <div class="razaoSocial">
       <a onclick="getValor(this.text);" href="#">Nicolas e Yuri Contábil ME</a>
    </div> 
    <div class="nomeFantasia">
        <a onclick="getValor(this.text);" href="#">ny contabilidade</a>
    </div> 
    <div class="razaoSocial">
        <a onclick="getValor(this.text);" href="#">Jennifer e Thiago Lavanderia Ltda</a>
    </div> 
    <div class="nomeFantasia">
        <a onclick="getValor(this.text);" href="#">jt lavanderia</a>
    </div>

Javascript :

function getValor(aa){
  console.info(aa);
}
  • 1

    thanks for the answer, I believe it would work perfectly

1

Without modifying its original structure, you can do this for the job;

function pegarValor(el){
  console.log(el.innerText);
}
<div class="conteudo-select" style="display: block;">
  <div class="razaoSocial">
    <a onclick="pegarValor(this); return false;">Nicolas e Yuri Contábil ME</a>
  </div> 
  <div class="nomeFantasia">
    <a onclick="pegarValor(this); return false;">ny contabilidade</a>
  </div> 
  <div class="razaoSocial">
    <a onclick="pegarValor(this); return false;">Jennifer e Thiago Lavanderia Ltda</a>
  </div> 
  <div class="nomeFantasia">
    <a onclick="pegarValor(this); return false;">jt lavanderia</a>
  </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.