Reputation: 1984
I have a aspx page and a js file, am accessing the js file for some functions, say for example I have some autocomplete boxes in my aspx page and for that am accessing the js file for every autocomplete box, and finally when I click the save button, the saveFunction in my js file should be fired, for this I just made my Save button as simple html button as
<button type="button" onclick="saveRecords()">Save</button>
at the end of my aspx page am calling the js file as
<script type="text/javascript">
$(document).ready(function () {
pageInit();
});
and in the pageInit of my js file I refer my saveRecords function as
function pageInit() {
saveRecords();
}
the problem here is whenever I run my application the saveRecords function is hitting first.....how can I avoid this, and call this function only when I hit Save button...can anyone help me here....
Upvotes: 0
Views: 624
Reputation: 639
Obviosly the saveRocords() will call when you call pageInit() in your code. You want your SaveRecords() to be executed while you click button,then place SaveRecords() along with pageInit(), and perform insert operation inside it.
It seems you are using jquery, then you can use following code to execute saveRecords
$('#buttonid').click(function(){
SaveRecords();
});
as you are using .aspx you need to know the clientID of button if you are using runat="server" ,to get this you can use following code in .js
var btnID =<%=buttonID.ClientID%>
$('#'+btnID).click(function(){
SaveRecords();
});
I hope this will help you
Upvotes: 1
Reputation: 1505
The document ready function will execute while the page load or each time you hit the refresh button..
If you want to call the function in the button click, remove the saveRecords from the pageInit() method..
Upvotes: 1
Reputation: 142921
If you don't want to call saveRecords
when the page initializes, you should remove the call to saveRecords
in the pageInit
function.
Upvotes: 1