0

Here is my javascript code but on every page load the javascript function get called i only wanted to call it when the button is clicked

            <input onclick="myFunction()"  id="btnAdd" type="button" class="btn-sm btn-default pull-right" value=" Add Qualification">

and here is my Javsacript function

<script type="text/javascript">


    function myFunction() {

        document.getElementById("btnAdd").onclick = function () { 
            $("#demo").load("@Html.Raw(Url.Action("_Experience", "Students")) ")
        };
    }
</script>
1
  • @Html.Raw() called when your page load because you are using Code behind inside your script it's not working on javascript or jquery event. Commented Oct 11, 2019 at 6:46

4 Answers 4

1

If you using

document.getElementById("btnAdd").onclick = function () { 
        $("#demo").load("@Html.Raw(Url.Action("_Experience", "Students")) ")
    };

this code then no need to use myFunction method and remove onclick from html. Or you can just write your login in myFunction method and document.getElementById("btnAdd").onclick and add onClick in html like that

<input onclick="myFunction()"  id="btnAdd" type="button" class="btn-sm btn-default pull-right" value=" Add Qualification">

<script type="text/javascript">
    function myFunction() {
        $("#demo").load("@Html.Raw(Url.Action("_Experience", "Students")) ")
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code, a simple solution

<input id="btnAdd" type="button" class="btn-sm btn-default pull-right" value="Add Qualification">

<script type="text/javascript">

    var Btnid = document.getElementById("btnAdd");

    Btnid.addEventListener('click', function(e) {
       alert('Event works only when it clicks')
    });

</script>

Comments

0
<script type="text/javascript">

        document.getElementById("btnAdd").onclick = function () { 
            $("#demo").load("@Html.Raw(Url.Action("_Experience", "Students")) ")
        };

</script>

just remove the myFunction()

1 Comment

avoid function if you are working with id base it's good habit either If you want get multiple times to get the same value then you can use function method.
0

This is the only thing you need. https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick

And use this, A self Executing Function which makes sure your function is not called on page load. But only when the button is clicked

// self executing function here
(function() {
   // your page initialization code here
    function myFunction() {
      document.getElementById("btnAdd").onclick = function () { 
        $("#demo").load("@Html.Raw(Url.Action("_Experience", "Students")) ")
      };
    } 
})();
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.