kesong
kesong

Reputation: 329

can not alert variable in html onclick

I can not using html onclick to alert a varible here is my html ,which used php to echo

    <?php
    $blog_owner='jean'; 

    echo '<span onclick="
    var blog_owner='.$blog_owner.';
    alert(blog_owner);


    " class="comment_r_button">Reply</span>';
   ?>

I find out if I assign number to $blog_owner, It works

Upvotes: 0

Views: 707

Answers (3)

Apul Gupta
Apul Gupta

Reputation: 3034

You should try this if you want to do this in the same way, you were doing:

<?php
$blog_owner='jean'; 

echo '<span onclick="
var blog_owner=\"'.$blog_owner.'\";
alert(blog_owner); " class="comment_r_button">Reply</span>';

?>

However, you can ignore the variable & can write it as:

<?php
$blog_owner='jean'; 

echo '<span onclick="alert(\"'.$blog_owner.'\"); " class="comment_r_button">Reply</span>';
?>

Upvotes: 0

Thomas
Thomas

Reputation: 1401

<!DOCTYPE html>
<html>
<head>
  <script>
    function showBlogOwner(){
      var blog_owner = 'jean';
      alert(blog_owner);
    }
  </script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <span onclick="showBlogOwner()" class="comment_r_button">Reply</span>
</body>
</html>

Upvotes: 0

Kyle Needham
Kyle Needham

Reputation: 3389

There is no need to create var to alert it try this.

echo '<span onclick="alert(' . $blog_owner . ')" class="comment_r_button">Reply</span>';

Upvotes: 1

Related Questions