Why is the jQuery selector ineffective in this situation?

Asked 1 years ago, Updated 1 years ago, 35 views

<script>
    function show_button2(){
        $('#demo') .html('<button>Button2</button>');
    }
</script>

<button onclick="show_button2();">Button1</button>
<divid='demo'></div>

I'd like you to click 'Button1' and get 'Button 2' (in <div> of id='demo' below).

But with the code above, when I click 'Button1' nothing comes out.

According to the code above, why is the jQuery selector ($('#demo')) ineffective?

How do I resolve this issue?

javascript jquery

2022-09-30 17:21

1 Answers

As BLUEPIXY said in the comments, there is a possibility that jQuery is not loaded.
Below is the same code as the questionnaire, but it works.

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

<script>
    function show_button2(){
        $('#demo') .html('<button>Button2</button>');
    }
</script>

<button onclick="show_button2();">Button1</button>
<divid='demo'></div> 


2022-09-30 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.