0

I'm trying to link this button with #analyzebutton id

<div class="pd-upload-foto">
     <div class="vertical-center text-center">
          <button id="startbutton" class='btn btn-ambilfoto btn-sm btn-1 font-3' style='padding-top:.2rem; padding-bottom:.2rem;' type='button'>ambil foto</button>
           <button id="analyzebutton" class='btn btn-sm btn-1 font-3' style='padding-top:.2rem; padding-bottom:.2rem; display: none;' type='button'>mulai analisis</button>
      </div>
</div>

<div class="pd-analisa" style="display: none;">
                <div class="vertical-center text-center">
                    <button id="analyzingbutton" class='btn btn-sm btn-1 font-3' style='padding-top:.2rem; padding-bottom:.2rem;' type='button'>mulai analisa</button>
                    <img id="tesfotomasuk" src="" alt="tes aja">
                </div>
 </div>

with this JQuery script

$(".btn-ambilfoto").click(function() {
     $(".btn-ambilfoto").html("berhasil").attr("disabled", "true");
     $(".btn-ambilfoto").removeClass("btn-1").addClass("btn-4");
     $("#analyzebutton").show();
});

$("#analyzebutton").click(function() {
     $(".pd-upload-foto").hide();
     $(".pd-analisa").show();

     $("#analyzingbutton").html("Analyzing...");
});

When I press the #analyzebutton button, it should fire the on click function and executes the function, but it doesn't. It's

I also have linked the JQuery script and the JS script at bottom of the html file.

Somehow, #analyzebutton is the only that doesn't work. The .btn-ambilfoto button (located just above the #analyzebutton) works without problem.

I've tried to change the code to $(selector).on("click", function(){}) but it doesn't work either.

How should I fix this? I've spent more than 6 hours to just to fix this.

12
  • I was trying to suggest that your issue is that the you have that element hidden with display: none. Commented Sep 8, 2020 at 17:15
  • 1
    "neither gives any output" - no error, no "0" or "1"? No output at all? Then I would suggest the code isn't being called. Commented Sep 8, 2020 at 17:40
  • 1
    $("#id").click will only apply to elements that exist at the time the code runs (assuming the code runs), so trying to establish that the element exists at that time, which it appears it doesn't, or your code is simply not running. $(document).on("click", "#analyzebutton", function() .. would the code to use if it's created later ("dynamically created"). But if you're code isn't even running... Commented Sep 8, 2020 at 17:51
  • 1
    Try control-F5 or open the page with console open and debugger set to disable cache (see stackoverflow.com/a/7000899/2181514) Commented Sep 8, 2020 at 18:42
  • 1
    Glad you found the issue (code not running) and hope you can find the cause. Commented Sep 8, 2020 at 18:42

2 Answers 2

0

Make sure your code is inside the document.ready function. That might be the problem.

$(document).ready(function(e) {
    $(".btn-ambilfoto").click(function() {
        $(".btn-ambilfoto").html("berhasil").attr("disabled", "true");
        $(".btn-ambilfoto").removeClass("btn-upself-1").addClass("btn-upself-4");
        $("#analyzebutton").show();
    });
    
    $("#analyzebutton").click(function() {
        $(".pd-upload-foto").hide();
        $(".pd-analisa").show();
    
        $("#analyzingbutton").html("Analyzing...");
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Tried this but it doesn't work.
0

The browser runs the previous saves (not the latest one) where it doesn't call #analyzebutton and calls another button instead. Thanks to @freedomn-m and this SOF answer, disabling the cache on the browser fixed the issue!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.