Tashen Jazbi Tashen Jazbi - 1 month ago 12
HTML Question

Button onclick event is not calling javascript function in php

I'm calling a javascript function in php. it works when i call alert() method directly on click. here is my code

echo "<form action='' method='post' enctype='multipart/form-data' >
<div class='fileupload fileupload-new' style='width: 250px;' data-provides='fileupload'><input type='hidden' value='' name=''>
<div class='fileupload-preview thumbnail' style='width: 390px; height: 250px; '></div>
<div>
<span class='btn btn-file btn-success'><span class='fileupload-new'>Select image</span>
<span class='fileupload-exists'>Change</span><input type='file' name=''></span>
<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"alert('hi');\">Save</button>
</div>
</div>
</form>";


this is the line which causing problem. it works for direct alert() method

<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"alert('hi');\">Save</button>


but when i call it through other javascript function, then this won't work.

<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"changeSitepic();\">Save</button>


here is the javascript code.

<script type="text/javascript">

function changeSitepic(){

alert('hi');

}

</script>


I unable to find where i'm doing wrong. please help.

Answer

I think your attribute 'id' and function name are same. Please change the 'ID' and try. Change your button like this and try. It may help you

 <button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic_id' onclick="return changeSitepic()">Save</button>