Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda