0

I typically don't use jquery but I have to use it to run an action. In my following code, the value is not alerted, making me believe there is a problem in my script. I have tried to look for a fix already but I couldn't find the error. Thanks for the help.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
    $(document).ready(function(){
           $("#formbtn1").click(function(e){
             e.preventDefault();
             var code = $('#code12').val();
             alert(code);
          });
});
</script>

The following is my HTML:

<form id="form">
   <input type="text" class="form-control" id="code12" name="installer1_code">
   <input type="submit" id="formbtn1" name="installer1_btn" class="btn btn-success">
            </form>
apaul
  • 308
  • 3
  • 13
  • 1
    You have a script tag with a src and contents. You should use 2 script tags, one for jquery and one for your inline script. – Jim Cote May 31 '17 at 01:06
  • 1
    Each ` – Jonathan Lonowski May 31 '17 at 01:06

2 Answers2

2

You cannot embed a script and in the same script tag have body. It's like you are saying load this and run this.

Modify your code as below

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
           $("#formbtn1").click(function(e){
             e.preventDefault();
             var code = $('#code12').val();
             alert(code);
          });
});
</script>
Nesar
  • 5,599
  • 2
  • 22
  • 21
1

You can't place code inside of a script tag and include a src attribute` as well.

You need to use two script tags. One for jQuery and one for your code.

<script src="link"></script>
<script>
//Your code
</script>
JJJ
  • 3,314
  • 4
  • 29
  • 43