• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

142
Vistas
How to get data attribute with "onclick" method?

I have following two "div" with "anchor" tag and i want to get "attribute" with "onclick" function,Here is my html code

<div class="upvote_dm">
<a class="pr_vote_up '.$clssup.' btnsnew '.$FeedId.'" data-datac="1" data-flagid='.$FeedId.' id=Up'.$FeedId.' data-type="feed" data-datacw='.$UserWalletAddress.' href="#"  data-id="10" onclick="return AddFeedVotes()"></a>
<span id="UpVoteCount'.$FeedId.'">'.$DefaultUpVotes.'</span>
</div>
                   
<div class="downvote_dm">
<a class="pr_vote_down '.$clssdown.' btnsnew '.$FeedId.'" data-datac="0" data-status="Down'.$FeedId.'" data-flagid='.$FeedId.' id=Down'.$FeedId.' data-type="feed" data-datacw='.$UserWalletAddress.' href="#" data-id="10" onclick="return AddFeedVotes()"></a>
<span id="DownVoteCount'.$FeedId.'">'.$DefaultDownVotes.'</span>
</div>

Now i am trying to get attribute with following way but showing me "Undefine",Where i am wrong ?

<script>
       function AddFeedVotes()
            {
                var vote = $(this).data('datac');
                var type = $(this).data('type');
                var flagid = $(this).data('flagid');
                            
                alert(vote);
                alert(type);
                 alert(flagid);    
            }
</script>
almost 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Maybe you can try native javascript

document.querySelector("#btn").addEventListener("click", event => {
  console.log(event.target.dataset.a)
})
<button data-a="111111" id="btn">button</button>

almost 3 years ago · Juan Pablo Isaza Denunciar

0

You haven't linked your inline calling of the function with the specific element, and therefore cannot reference the data attributes.

You can add a click event using a new class as I have done below, if you add .vote to your links then add a click event to them all using jquery.

$("a.vote").click( function() {
   // code goes here
});

You will need to distinguish up or down votes later in your code, but could use:

if ($(this).hasClass('pr_vote_down')) {
   // down vote code
} else {
   // up vote code
}

// Add click event to all a tags with class vote
$("a.vote").click(function() {

  // No you can use $(this) as you have linked the function to the specific element rather than calling it in the absract.
  var vote = $(this).data('datac');
  var type = $(this).data('type');
  var flagid = $(this).data('flagid');

  // Check if the clicked link has the down vote class
  if ($(this).hasClass('pr_vote_down')) {
    // down vote code
    alert("down vote");
  } else {
    // up vote code
    alert("up vote");
  }

  alert(vote);
  alert(type);
  alert(flagid);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="upvote_dm">
  <a class="pr_vote_up vote '.$clssup.' btnsnew '.$FeedId.'" data-datac="1" data-flagid='.$FeedId.' id=Up '.$FeedId.' data-type="feed" data-datacw='.$UserWalletAddress.' href="#" data-id="10">Up Vote</a>
  <span id="UpVoteCount'.$FeedId.'">'.$DefaultUpVotes.'</span>
</div>

<div class="downvote_dm">
  <a class="pr_vote_down vote '.$clssdown.' btnsnew '.$FeedId.'" data-datac="0" data-status="Down'.$FeedId.'" data-flagid='.$FeedId.' id=Down '.$FeedId.' data-type="feed" data-datacw='.$UserWalletAddress.' href="#" data-id="10">Down Vote</a>
  <span id="DownVoteCount'.$FeedId.'">'.$DefaultDownVotes.'</span>
</div>

almost 3 years ago · Juan Pablo Isaza Denunciar

0

In your code you didn't added anything for a tag.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <div class="upvote_dm">
        <a  id="upVote" class="pr_vote_up '.$clssup.' btnsnew '.$FeedId.'" data-datac="1" data-flagid='.$FeedId.' id=Up'.$FeedId.' data-type="feed" data-datacw='.$UserWalletAddress.' href="#"  data-id="10" onclick="return AddFeedVotes()">Up vote</a>
        <span>'.$DefaultUpVotes.'</span>
    </div>

    <div class="downvote_dm">
        <a id="downVote" class="pr_vote_down '.$clssdown.' btnsnew '.$FeedId.'" data-datac="0" data-status="Down'.$FeedId.'" data-flagid='.$FeedId.' id=Down'.$FeedId.' data-type="feed" data-datacw='.$UserWalletAddress.' href="#" data-id="10" >Down vote</a>
        <span >'.$DefaultDownVotes.'</span>
    </div>
</body>
</html>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript">

    $('#upVote, #downVote').click(function(event) {
        var vote = $(this).data('datac');
        var type = $(this).data('type');
        var flagid = $(this).data('flagid');

        alert(vote);
        alert(type);
        alert(flagid);  
    });


</script>
almost 3 years ago · Juan Pablo Isaza 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda