0

So have a script named script.js:

var newLabel = '';
$('#payment_amount').on('change', function(){
    $('#change_label').text(newLabel); //Change the text before changing the value
    switch(this.value){
        case 'junior':
            newLabel = 'Junior Account';
            break;
        case 'premium':
            newLabel = 'Premium Account';
            break;
    }
}).trigger('change');

I'm calling it to my html file by:

<script type="text/javascript" src="script.js"></script>

My code for select is this:

<select id = "payment_amount">
    <option class="junior" value="junior">Junior</option>
    <option class="premium" value="premium">Premium</option>
</select>

<label id="change_label">Hello</label>

I already tried directly putting my js code to the html by adding it to the tag but I'm still not having any luck. Where did I go wrong with this one?

7
  • $ is jquery.. Pease add the jquery script as cdn or from your local. Commented Feb 8, 2018 at 5:21
  • Working fine on my end. What do you mean by not working? What are you trying to achieve? Commented Feb 8, 2018 at 5:22
  • @HemaNandagopal hmmm sorry for the newb question but how can I add that to my script? Commented Feb 8, 2018 at 5:23
  • @Eddie the label doesnt change to anything even though i change the select value? i have no idea what i am missing. Commented Feb 8, 2018 at 5:24
  • Did you add jquery? Commented Feb 8, 2018 at 5:24

7 Answers 7

2

You need to add jquerylibrary to your code, Have it locally (or) point to any CDN like below,

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

Here is a working version of your code,

https://jsfiddle.net/uqo84q71/

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0
   <script type="text/javascript"> 
 var newLabel = ''; $('#payment_amount').on('change', function(){ $('#change_label').text(newLabel); //Change the text before changing the value switch(this.value){ case 'junior': newLabel = 'Junior Account'; break; case 'premium': newLabel = 'Premium Account'; break; } }).trigger('change'); 
</script>

If its work then check the src attributes (script.js) is it one same folder-

Comments

0

First of all add Main jquery file. Jquery file

2ndly add this replace this code.

 $(document).ready( function(){
       var newLabel = '';
        $('#payment_amount').on('change', function(){
        $('#change_label').text(newLabel);
    switch(this.value){
    case 'junior':
        newLabel = 'Junior Account';
        break;
    case 'premium':
        newLabel = 'Premium Account';
        break;
   }
   }).trigger('change');
 });

Comments

0

Modified your HTML a little. See where I've included the scripts. Try it and check if it works now.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <select id = "payment_amount">
        <option class="junior" value="junior">Junior</option>
        <option class="premium" value="premium">Premium</option>
    </select>
    <label id="change_label">Hello</label>
    <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="1.js"></script>
</body>

Comments

0

In your case ,

Check these 2 things .

1) Import Jquery cdn

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

2)Check the file of script.js where it was added i.e path of it. Might be path related issues.

Hope this helps..!

Comments

0

Try to put it on head:-

## <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="1.js"></script>
</head>
<body>
<select id = "payment_amount">
    <option class="junior" value="junior">Junior</option>
    <option class="premium" value="premium">Premium</option>
</select>
<label id="change_label">Hello</label>
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
</body>

##

Comments

0
var newLabel = '';
$('#payment_amount').on('change', function(){
    $('#change_label').text(newLabel); //Change the text before changing the value
    switch($('#payment_amount').val()){ // add jquery-3.2.1.min.js and change this
        case 'junior':
            newLabel = 'Junior Account';
            break;
        case 'premium':
            newLabel = 'Premium Account';
            break;
    }
})

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.