Support » Fixing WordPress » Post id in javascript

  • I’d like to know how to call a post id from a javascript file.
    In the template I use <?php the_ID(); ?> but how can I call the same id from Javascript?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I don’t really get what you are trying to do but I will attempt to answer it.

    function the_ID(){
       return "<?php the_ID(); ?>";

    ok, thanks for your answer. The problem is I’m trying to call the id from an external javascript file. I do this wich is wrong:

    document.getElementById("message_<?php the_ID(); ?>")

    and I get > “document.getElementById(“message_<?php the_ID(); ?>”) has no properties” in Firebug.

    what am I doing wrong?

    Are you calling the JS before the content appears? Use window.onload.
    Post your blog’s URL by the way.

    I’m trying this:

    <script type="text/javascript">
    function the_ID(){
    alert("<?php the_ID(); ?>");

    And when I click on this link

    <a href="#" onclick="the_ID();">CLICK</a>

    I get always the value 18 no matter what post I click on.


    You should’ve just post your URL because:
    #1: You are doing it wrong.
    #2: You are doing it wrong.

    It should be something like this for a standard WordPress blog.

    function get_post_content(id){
       return document.getElementById("post-"+id).innerHTML;
    }//you should probably use textContent/innerText but I am not going to get into that here
    <a href="#" onclick="alert(get_post_content(<?php the_ID(); ?>));">CLICK</a>

    i love you haochi.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Post id in javascript’ is closed to new replies.