Member Avatar for krushed18

My problem is that I am trying to execute a function on button click event but it just shows not even for a sec it just flashes.

html & Javascript :

<html>
    <head>
    <script type="text/javascript">
    function createLabel(){

    /*Get UserName*/
    var unString = document.createElement("label");
    unString.className = "userName";
    var uName = document.getElementById("username").innerHTML;
    unString.innerHTML = "By <a href='profile.php'>" + uName + "</a>";
    /*Get UserName*/


    /*Get PostString*/
    var postString = document.createElement("label"); 
    var postText = document.getElementById("poster").value;       
    postString.className = "postText";
    postString.innerHTML = "\n\n" + postText;
    postString.style.display = "block";
    /*Get PostString*/

    var hr = document.createElement("hr");


    var tr = document.createElement("tr");
    var td = document.createElement("td");
    tr.className="postTR";
    td.className = "postTD";
    td.appendChild(unString);
    td.appendChild(postString);
    td.appendChild(td);
    tr.appendChild(hr);
    document.getElementById("postTable").appendChild(tr);

    }

    </script>
    </head>

    <body>

    <div id="wrapper">

    <form id="form1" method="POST" action="">

    <textarea id="poster" name="postbox" rows="4" style="position: absolute; resize:none; margin-left:450px; margin-top:70px;" cols="70"></textarea>
    <input type="submit" onclick="createLabel();" value="Post" style="position: absolute; margin-top:160px; margin-left:990px;" id="submit" name="submit" />
    <div id="hrln1" style="position:absolute; margin-top:180px; width:1050px; margin-left:0px;"><hr /></div>
    </form>

    <a href="profile.php"><label id="username" style="cursor:pointer;"><?php echo user_details('username'); ?></label></a>

    <div id="postContainer">
    <table id="postTable" cellpadding="5">          
    </table>
    </div>
    </div>  
    </body>
    </html>
Member Avatar for stbuchok

The button is causing a postback. Return false from your function to cancel the postback. Or have:

onclick="blah(); return false;"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.