user3105607
user3105607

Reputation: 369

JavaScript event listener not being added to button

I have a <button> in my code that has the id button_edit_post, and my java script won't add the event listener to it.

document.onload = function(){
    document.getElementById('button_edit_post').addEventListener('click', function(){
        alert("event listener added");
        var data = document.getElementById('edit_post').innerHTML;
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert("data added");
            };
        };
        var php = data + "q=" + post_id;
        console.log(php);
        xhr.open("POST", "edit_post.php?p="+php, true);
        xhr.send();
    }, false);
};

HTML:

<article>Edit blog post
    <section><div id="admin_subtitle">Select the post you want to edit then modify the text</div>
            <select name="edit" id='edit' onchange='editPost(value)'>
                    <option>Please select a post..</option>
                    <?php
                    $result = mysql_query("SELECT BlogPostID, BlogPostTitle FROM blogpost ORDER BY BlogPostID DESC");
                    while ($row = mysql_fetch_array($result)){
                        $blogvar  = array($row ['BlogPostID'], $row ['BlogPostTitle']);
                    ?>
                    <option value="<?php echo $blogvar[0]; ?>" id='edit_option'><?php echo $blogvar[1]; ?></option>
                    <?php
                    };
                    ?>
                </select>
                <div id='gif_loader'></div>
                <div id='text_edit_area' style='display: none;'>
                    <textarea id='edit_post' rows='30' cols='200'></textarea>
                    <button id='button_edit_post'>Edit Post</button>
                </div>

any help would be great,

Thanks :)

Upvotes: 1

Views: 343

Answers (1)

StackSlave
StackSlave

Reputation: 10627

Should be window.onload = function, or really just onload = function, since window is implicit.

Upvotes: 2

Related Questions