Raoul
Raoul

Reputation: 1892

Why can't I disable my button?

I've been trying for hours to disable a simple javascript button (id="storeButton") in this short script. I have tried every variation in syntax found on the internet to that effect, but to no avail. I am executing this in a wordpress page. Can someone please tell me what is my mistake?

The wordpress page:

<p style="text-align: center;">Welcome!</p>
<strong>What is this project, and why contribute?</strong>

text

&nbsp;

<script src="/scripts/alert.js" type="text/javascript"></script>
<script src="/webcamjs/webcam.js" type="text/javascript"></script>
<script src="/scripts/take_snapshot.js" type="text/javascript"></script>

<div class="centre" id="my_camera" style="width:320px; height:240px;"></div>
<br>
<div id="freezeButton"></div>
<div id="storeButton"></div>
<br>
<div id="my_result"></div>

<script type="text/javascript">
<!--
ShowAlert();
webcamConfigure();
attach();
createFreezeButton();
disableBtn();
createStoreButton();
//-->
</script>

take_snapshot.js, where the problem seems to be

document.getElementById("storeButton").disabled = true;:

function webcamConfigure(){
    Webcam.set({
        width: 320,
        height: 240,
        dest_width: 640,
        dest_height: 480,
        image_format: 'jpeg',
        jpeg_quality: 100,
        force_flash: true
    });
}

function attach(){
    Webcam.attach( '#my_camera' );
}

function createStoreButton(){
    sendButton = document.createElement("input");
    sendButton.type = "button";
    sendButton.value = "Send snapshot!";
    sendButton.onclick = function store(){
        Webcam.snap( function(data_uri) {
            document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>';
        } );
    }
    placeHolder = document.getElementById("storeButton");
    placeHolder.appendChild(sendButton);
}

function disableBtn(){
    document.getElementById("storeButton").disabled = true;
}

function createFreezeButton(){
    myButton = document.createElement("input");
    myButton.type = "button";
    myButton.value = "Take snapshot!";
    myButton.onclick = function freeze(){
        if (myButton.value=="Take snapshot!") {
            myButton.value = "Discard snapshot!";
            Webcam.freeze();
            //document.getElementById("storeButton").disabled = false;
        }
        else {
            myButton.value = "Take snapshot!";
            Webcam.unfreeze();
            //document.getElementById("storeButton").disabled = true;
        }
    }
    placeHolder2 = document.getElementById("freezeButton");
    placeHolder2.appendChild(myButton);
}

Upvotes: 0

Views: 210

Answers (2)

Raoul
Raoul

Reputation: 1892

Well, someone transiently posted a downvoted answer stating that my button was working, but that it was lacking css. It wasn't a completely right answer, but it seems he had some good points, because that sent me on the right track. (If you are reading this, dear former answerer, re-publish your answer and I'll upvote it!).

I don't really know what I'm doing, here... and the structure of my code is certainly a blasphemy against the gods of JS, however it works, so here goes:

var sendButton;
var freezeButton;

function webcamConfigure(){
    Webcam.set({
        width: 320,
        height: 240,
        dest_width: 640,
        dest_height: 480,
        image_format: 'jpeg',
        jpeg_quality: 100,
        force_flash: true
    });
}

function attach(){
    Webcam.attach( '#my_camera' );
}

function createStoreButton(){
    sendButton = document.createElement("input");
    sendButton.type = "button";
    sendButton.value = "Send snapshot!";
    sendButton.disabled = true;
    sendButton.onclick = function store(){
        Webcam.snap( function(data_uri) {
            document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>';
        } );
        freezeButton.value = "Take snapshot!"
        sendButton.disabled = true;
    }
    placeHolder = document.getElementById("storeButton");
    placeHolder.appendChild(sendButton);
}

function createFreezeButton(){
    freezeButton = document.createElement("input");
    freezeButton.type = "button";
    freezeButton.value = "Take snapshot!";
    freezeButton.onclick = function freeze(){
        if (freezeButton.value=="Take snapshot!") {
            freezeButton.value = "Discard snapshot!";
            Webcam.freeze();
            sendButton.disabled = false;
        }
        else {
            freezeButton.value = "Take snapshot!";
            Webcam.unfreeze();
            sendButton.disabled = true;
        }
    }
    placeHolder2 = document.getElementById("freezeButton");
    placeHolder2.appendChild(freezeButton);
}

Upvotes: 0

David P
David P

Reputation: 2093

You are trying to disable the div called "storeButton", instead of disabling the button you created that is contained inside that div. In your context, a simple way to do that is to have a public var when creating the button, that is available when you want to disable it:

var sendButton;
function webcamConfigure(){
    Webcam.set({
        width: 320,
        height: 240,
        dest_width: 640,
        dest_height: 480,
        image_format: 'jpeg',
        jpeg_quality: 100,
        force_flash: true
    });
}

function attach(){
    Webcam.attach( '#my_camera' );
}

function createStoreButton(){
    sendButton = document.createElement("input");
    sendButton.type = "button";
    sendButton.value = "Send snapshot!";
    sendButton.onclick = function store(){
        Webcam.snap( function(data_uri) {
            document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>';
        } );
    }
    placeHolder = document.getElementById("storeButton");
    placeHolder.appendChild(sendButton);
}

function disableBtn(){
    sendButton.disabled = true;
}

Hope this helps,

:) David

Upvotes: 1

Related Questions