Getting [object Object] by passing php string to onclick function using jquery Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Getting [object Object] by passing php string to onclick function using jquery without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want to alert hello instead I’m getting [object Object]. Getting [object Object] by passing php string to onclick function using jquery

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<p id="demo"></p>

<a class="readmore" onclick="myFunction('<?php echo "hello";?>')">Readmore....</a>

<?php
call();
function call(){
?>
<script>
$(document).ready(function(){
  $(".readmore").click(function(link){
    alert(link);
  });
}); 
</script>
<?php }
?>
</body>
</html>

Answer

The Problem is your JavaScript code. You are calling a function, which does not exist. That’s why it doesn’t do anything. If you want to do it in jQuery you will have to define link. Otherwise, link is equal to [object Object], because link is passed by a function, which is called without the attribute link.

Fixed:

  <!DOCTYPE html>
    <html>
    <body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <p id="demo"></p>
    
    <a class="readmore" onclick="show_alert('<?php echo "hello";?>')">Readmore....</a>
    
    <?php
    call();
    function call(){
    ?>
    <script>
    function show_alert(link) {
      alert(link);
    }
    </script>
    <?php }
    ?>
    </body>
    </html>

With Jquery:

  <!DOCTYPE html>
    <html>
    <body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <p id="demo"></p>
    
    <a class="readmore" link="test">Readmore....</a>
    
    <?php
    call();
    function call(){
    ?>
    <script>
      $(".readmore").click(function(){
      link = $(".readmore").attr("link");
       alert(link);
      });
    </script>
    <?php }
    ?>
    </body>
    </html>
We are here to answer your question about Getting [object Object] by passing php string to onclick function using jquery - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji