Search code examples
javascriptjqueryhtmlajaxsend

Sending input data to a javascript file


My following code is this:

<form>
    <input class="inputField" type="text" required="required" pattern="[a-zA-Z]+" />
    <input class="myButton" type="submit" value="Submit" />
</form>

<script type="text/javascript">
    $(document).ready(function () {
        $(".myButton").on("click", function () {
            var value = $(".inputField").val();
        });
    });
</script>

I want to send to the file main.js the data after a user puts in some information in the input and clicks on the submit button. I tried with an ajax() method but it doesn't work or I don't know how to do this. After I send it to the main.js file I want to access that information. Anyone have an idea of how to do it?


Solution

  • You can "send" the input field value to main.js by passing the input field value as a parameter to a function in main.js.

    main.js:

    function mainjsfunction(inputFieldValue)
    {
        console.log(inputFieldValue);  
        //do something with input field value
    }
    

    html:

    <form>
        <input class="inputField" type="text" required="required" pattern="[a-zA-Z]+" />
        <input class="myButton" type="submit" value="Submit" />
    </form>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $(".myButton").on("click", function () {
                var value = $(".inputField").val();
                mainjsfunction(value);
            });
        });
    </script>