Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to execute a script at the click of a button?

$
0
0

Project structure - see picture.
How to make the text "test" move to the right after each button click?
How to do this using elements:
- <a>
- <button> Start js </button>
- <input type = "submit" value = "Start js" />
 
 
ScriptTest1.js (preliminary example)

var divxPos = 0;
function runCode ()
{     element = document.getElementById ("testElement");     element.style.left = divxPos ++ + 'px';     setTimeout (() => runCode (element), 50);
}

Index.cshtml

<body>     <p id = "testElement" style = "position: absolute"> test </p></body>





Update 1
  How to run a script with the click of a button if the script is located in a separate file?
Condition:
- the script is located in a separate file;


Update 2
View1.cshtml- not working
Chrome -> F12 -> Sources - I do not see the file "ScriptTest2.js"
Code

@section Scripts
{     <script src = "~ / js / ScriptTest2.js"> </script>
}<body>     <button type = "button" id = "btnMove" onclick = "runCode ();"> Move it </button>     <div id = "testElement" style = "padding-left: 1px;"> test </div></body>

When I enter the path with my hands, the file "ScriptTest2.js" is displayed in the prompts.

View2.cshtml - works
Chrome -> F12 -> Sources - I see the file "ScriptTest2.js"

Code

<script src = "~ / js / ScriptTest2.js"> </script><body><button type = "button" id = "btnMove" onclick = "runCode ();"> Move it </button><div id = "testElement" style = "padding-left: 1px;"> test </div></body>

Question.
Why when I use "@section Scripts" the script does not appear in "Chrome -> F12 -> Sources"?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>