How do I get content from a div with the same class?

0

I think the title is quite self-explanatory but come on, I have a return from 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 need to get the content inside the tag to ...

I tried with data = event.srcElement.innerText; but it does not work in Firefox, nor does it put parameter in function function pegarValor(event) {

Is there any way to do this that I need?

    
asked by anonymous 28.07.2017 / 16:00

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);
}
    
28.07.2017 / 16:06
1

Without changing its original structure, you can do it by passing this to the functions;

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>
    
28.07.2017 / 16:06