javascript - Uncaught TypeError: $(...).ready(...) is not a function even though it works on JsFiddle - Stack Overflow

The JSFiddle in question seems to have no trouble:(if you type a url like google it will select the

The JSFiddle in question seems to have no trouble: / (if you type a url like google it will select the value of "" or whatever you put after the dot and it is in the list)

However the jQuery function seems to be faulty?

In chrome I get this error:

Uncaught TypeError: $(...).ready(...) is not a function

Here is my code:

<html>
<head>
    <title>Add Site</title>

    <script src="js/jquery-3.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/msdropdown/dd.css" />
    <script src="js/msdropdown/jquery.dd.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/msdropdown/flags.css" />
</head>
<body>
<h1>Add new site</h1>
<div class="contentbox">
<form method="post">
        <input type="hidden" name="addsite" value="true"/>
            <p>
            <label for="site_url">Site url:</label>
            <input type="text" name="site_url" id="urlText" placeholder="domain.xxx" value=""/>
        </p>
        <p>
        <label for="site_url">Search locale:</label>
        <select name="locale" id="locale">
            <option value="">
              Select locale
            </option>
             <optgroup label="Popular">
<option value=".dk" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag dk" data-title="Denmark">Denmark - Danish</option>
                                                                                <option value=".de" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag de" data-title="Germany">Germany - German</option>
  <option value=".au" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag au" data-title="Australia">Australia - English</option>

  </optgroup>
            </select>
          </p>

        <p>
            <label for="site_url"></label>
            <input type="submit" name="submit"  class="btn" value="Add">
        </p>


</form>
</div>


</body>
</html>

<script>
$(document).ready(function() {
$("#locale").msDropdown();
})


(function ($) {
    $('#urlText').on('change', function () {
        var value = this.value,
            parts = this.value.split('.'),
            str, $opt;
        for (var i = 0; i < parts.length; i++) {
            str = '.' + parts.slice(i).join('.');
            $opt = $('#locale option[value="' + str + '"]');
            if ($opt.length) {
                $opt.prop('selected', true);
                break;
            }
        }
    })
})(jQuery);
</script>

What I want to do is when a user types a url, like "google.dk" , it should select the value with ".dk" at the end from the dropdown for him.

The JSFiddle in question seems to have no trouble: http://jsfiddle/S3LF3/ (if you type a url like google. it will select the value of "." or whatever you put after the dot and it is in the list)

However the jQuery function seems to be faulty?

In chrome I get this error:

Uncaught TypeError: $(...).ready(...) is not a function

Here is my code:

<html>
<head>
    <title>Add Site</title>

    <script src="js/jquery-3.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/msdropdown/dd.css" />
    <script src="js/msdropdown/jquery.dd.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/msdropdown/flags.css" />
</head>
<body>
<h1>Add new site</h1>
<div class="contentbox">
<form method="post">
        <input type="hidden" name="addsite" value="true"/>
            <p>
            <label for="site_url">Site url:</label>
            <input type="text" name="site_url" id="urlText" placeholder="domain.xxx" value=""/>
        </p>
        <p>
        <label for="site_url">Search locale:</label>
        <select name="locale" id="locale">
            <option value="">
              Select locale
            </option>
             <optgroup label="Popular">
<option value=".dk" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag dk" data-title="Denmark">Denmark - Danish</option>
                                                                                <option value=".de" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag de" data-title="Germany">Germany - German</option>
  <option value=".au" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag au" data-title="Australia">Australia - English</option>

  </optgroup>
            </select>
          </p>

        <p>
            <label for="site_url"></label>
            <input type="submit" name="submit"  class="btn" value="Add">
        </p>


</form>
</div>


</body>
</html>

<script>
$(document).ready(function() {
$("#locale").msDropdown();
})


(function ($) {
    $('#urlText').on('change', function () {
        var value = this.value,
            parts = this.value.split('.'),
            str, $opt;
        for (var i = 0; i < parts.length; i++) {
            str = '.' + parts.slice(i).join('.');
            $opt = $('#locale option[value="' + str + '"]');
            if ($opt.length) {
                $opt.prop('selected', true);
                break;
            }
        }
    })
})(jQuery);
</script>

What I want to do is when a user types a url, like "google.dk" , it should select the value with ".dk" at the end from the dropdown for him.

Share Improve this question asked Feb 17, 2017 at 12:53 crystyxncrystyxn 1,6137 gold badges36 silver badges74 bronze badges 7
  • 2 have you included your jquery file and is it in the correct place and loaded? – Pete Commented Feb 17, 2017 at 12:56
  • yes, it loads jquery just fine – crystyxn Commented Feb 17, 2017 at 12:56
  • It did not change anything – crystyxn Commented Feb 17, 2017 at 12:58
  • What happens if you remove the document ready function and add the $("#locale").msDropdown(); above the $('#urlText') in the other function? – Pete Commented Feb 17, 2017 at 13:03
  • I managed to get rid of the error with Flanagan's suggestion. But the script is not doing anything it seems. – crystyxn Commented Feb 17, 2017 at 13:05
 |  Show 2 more ments

3 Answers 3

Reset to default 2

I played with your code and found out, it's the missing semicolon at the end of $(document).ready();

    $(document).ready(function() {
         $("#locale").msDropdown();
    });

Just try it in the built-in editor:

<html>
<head>
    <title>Add Site</title>

    <script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/msdropdown/dd.css" />
    <script src="js/msdropdown/jquery.dd.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/msdropdown/flags.css" />
</head>
<body>
<h1>Add new site</h1>
<div class="contentbox">
<form method="post">
        <input type="hidden" name="addsite" value="true"/>
            <p>
            <label for="site_url">Site url:</label>
            <input type="text" name="site_url" id="urlText" placeholder="domain.xxx" value=""/>
        </p>
        <p>
        <label for="site_url">Search locale:</label>
        <select name="locale" id="locale">
            <option value="">
              Select locale
            </option>
             <optgroup label="Popular">
<option value=".dk" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag dk" data-title="Denmark">Denmark - Danish</option>
                                                                                <option value=".de" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag de" data-title="Germany">Germany - German</option>
  <option value=".au" data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag au" data-title="Australia">Australia - English</option>

  </optgroup>
            </select>
          </p>

        <p>
            <label for="site_url"></label>
            <input type="submit" name="submit"  class="btn" value="Add">
        </p>


</form>
</div>


</body>
</html>

<script>
$(document).ready(function() {
//$("#locale").msDropdown();
});


(function ($) {
    $('#urlText').on('change', function () {
        var value = this.value,
            parts = this.value.split('.'),
            str, $opt;
        for (var i = 0; i < parts.length; i++) {
            str = '.' + parts.slice(i).join('.');
            $opt = $('#locale option[value="' + str + '"]');
            if ($opt.length) {
                $opt.prop('selected', true);
                break;
            }
        }
    })
})(jQuery);
</script>

The JQuery shortcut - $() - seems to have been overridden by another library is my guess.

Try using:

jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
});

The reason the JSFiddle works fine is it doesn't actually use the country flag drop down menu plugin you found. The way the plugin works is different than the way a standard <select> html tag works.

I looked inside the jquery.dd.js file, and it seems there is no way to interact with the dropdown menu using javascript. Therefore, your line $opt.prop('selected', true); is valid for a real drop down menu (a <select> tag), but not for the plugin's drop down menu, which handles the selected state differently. Also, the plugin provides little documentation and has little to no ments in the code, so this makes it diffcult to know how the plugin works.

I can see 2 solutions to your problem:

  1. You can try to understand the plugin's code in the jquery.dd.js file and try to implement a way to tell the plugin which element you want to select. I wouldn't normally remend editing third-party plugins, but since the site you got the plugin from has not been updated since 2012, there is little chance for the plugin to receive updates in the future, so editing it would be safe in this case. It will require some knowledge in jQuery plugin making, though.

  2. You can try to find another country flag drop down plugin that can be updated using javascript instead of using your mouse only. Such projects can be found on Github (here are some results found on Github: Country flag drop down menu results on Github).

Previous information found:

From the code you provided, you are missing a semi-colon after the call of the $(document).ready() function. It should look like this:

$(document).ready(function() {
    $("#locale").msDropdown();
}); // <--- This semi-colon is missing

Also, the code in (function ($) {// code} )(jQuery); may be executed before the document was ready, which means this code could sometimes not work. Try putting this code inside your $(document).ready(); anonymous function. This way, you will be sure all html nodes will be accessible by jQuery.

Finally, you should consider putting your javascript logic somewhere inside the <html> tag (inside the <head>, right after <body> or right before </body>). While putting the <script> tag after the </html> tag may work at first, this is far from valid html and may be the cause of some problems you're having. The best way would be to put your javascript in a .js file and link it in your <head> the same way you linked jQuery and msdropdown:

<head>
    <!-- Your other linked files... -->
    <script src="js/main-logic.js"></script>
</head>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745627591a4636903.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信