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

189
Visualizações
Get Div value when clicked using AJAX
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">

I am trying to get value of div where ID is Class when Div is clicked.

function caller(){
          $.ajax({
              url: "test.php",
              type: "get",
              data: {
                Class_info: $('#Class').val()
              }
          }).done(function(data) {
              $('#Result').text(data);
              alert(data);
        });
      }

Looks like it isn't reading the value of Div when clicked. Am I using AJAX wrongly?

test.php

<?php
  $a = $_GET['Class_info'];
  echo($a);
?>
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

div tag dont have value attribute. So you can use $('#Class').attr('value'); check snippet below..

function caller(){
  alert($('#Class').attr('value'));
  $.ajax({
      url: "test.php",
      type: "get",
      data: {
        Class_info: $('#Class').attr('value')
      }
  }).done(function(data) {
      $('#Result').text(data);
      alert(data);
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">test</div>

over 4 years ago · Santiago Trujillo Relatório

0

You can use this little trick:

<div id "" class="Class_ticket" onclick="caller()" value="1">
    <input type="hidden" value="1" id="Class" />
</div>

note that the input is hidden!

And here is your ajax:

function caller() {
    $.ajax({
        url: "test.php",
        type: "get",
        data: {
            Class_info: $('#Class').val()
        }
    }).done(function(data) {
        $('#Result').text(data);
        alert(data);
    });
}
over 4 years ago · Santiago Trujillo Relatório

0

Try this:

function caller(){
          $.ajax({
              url: "test.php?Class_info=test",
              type: "get",
              data: {
                Class_info: $('#Class').val()
              }
          }).done(function(data) {
              $('#Result').text(data);
              alert(data);
        });
      }

I added ?info=test which in theory PHP should be able to take

if PHP takes the value, then instead of test put a variable containing the desired value

over 4 years ago · Santiago Trujillo 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