2

I need build code such : http://fiddle.jshell.net/bwKZt/152/ but my code dosen't work ! I am a beginner. Please help me.

index.php:

<!DOCTYPE html><html>

    <head>
      <script>
        $("#label").bind("keyup", changed).bind("change", changed);
        function changed() {
          $("#url").val(this.value);
        }
      </script>
    </head>

    <body>
      <input type="text" id="label" />
      <input type="text" id="url" readonly />
    </body>

</html>

2 Answers 2

3

Some of the JavaScript here isn't native JavaScript , but is using a plugin called jQuery that makes searching and manipulating HTML elements easier.

When you see $(), that's the jQuery way of finding elements. But it won't work because you don't have jQuery referenced at all.

If you don't want to use jQuery, you can find elements with something like document.getElementById('label').

But lots of people use jQuery to make referencing page elements short and sweet, as with $('#label').

Try to reference jQuery first, like:

<!DOCTYPE html><html>

    <head>
      <!-- The below line references an externally hosted copy of jQuery 2.2.4 -->
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

      <script>
        // The below chunk is telling it to bind to the keyup event only AFTER the document has fully loaded.
        // Sometimes when your binding code is executed, the elements you wish to bind to aren't loaded yet. 
        $(document).ready(function(){
            $("#label").bind("keyup", changed).bind("change", changed);
        });

        function changed() {
          $("#url").val(this.value);
        }
      </script>
    </head>

    <body>
      <input type="text" id="label" />
      <input type="text" id="url" readonly />
    </body>

</html>
Sign up to request clarification or add additional context in comments.

2 Comments

"keyup" and "change" is event for run function changed(). Are there events to start copy value of "label" and paset to "url" ?
@ArmanDownload, after jQuery is referenced, then that's already what your code does. When keyup executes on #label, changed() runs, which takes the changing element's value and puts it into #url
0

The first problem is because you haven't include jquery with a script tag to solve that add this code in the head of you html file if you have the Internet connection to load Jquery from CDN

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

or you can download Jquery file from Jquery site and you will have it locally

after that you must execute this code after the executing the jquery ready function

$(document).ready(function(){
    $("#label").bind("keyup", changed).bind("change", changed);
    function changed() {
       $("#url").val(this.value);
    }
})

1 Comment

"keyup" and "change" is event for run function changed(). Are there events to start copy value of "label" and paset to "url" ?

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.