Skip to content

Is it a bug? - I can not get the correct state of the switchery when loading the page for the first time #164

@martinmurciego

Description

@martinmurciego

The initial state of the switchery is always false when it is obtained by Javscript / jQuery code, when the page is loaded or rendered having this checked / unchecked value preset in the HTML code.
I want to retrieve the current state (checked / unchecked) to use in a validation of data entered into a form.

Javascript / Jquery 3.2.1

if( $('#checkIsIndoor').prop('checked')===true) {
if ( $("#inputHeight").val().trim() === '' ){
                showError('inputHeight', 'As it is an Interior Meeting Space you must enter the height in meters.');
                todoOk = false;
            }
            else{
                clearErrors('inputHeight');
            }
}

HTML

<div class="form-group">
    <label>
        <input id="checkEsInterior" name="esInterior" type="checkbox" class="js-switch" checked="" data-switchery="true" style="display: none;"> Es Interior?
    </label>
</div>

However, after clicking on the Switchery and re-executing the code that gets the value of the Switchery / Checkbox, the value is correct.
The only thing that works for me correctly and should not be is:

if ($("#checkEsInterior + span.switchery-default").css('background-color')=="rgb(38, 185, 154)"){
if ( $("#inputHeight").val().trim() === '' ){
                showError('inputHeight', 'As it is an Interior Meeting Space you must enter the height in meters.');
                todoOk = false;
            }
            else{
                clearErrors('inputHeight');
            }
}

The color rgb (38, 185, 154) is the checked status color of the Switchery by default.

Why does this happen?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions