Reputation: 87
I am trying to use html/css with my jquery, but can not get it to work. as of right now when I click on a div that matches the "vlabe" it will turn on or off depending on the state. however I am trying to use a html/css on/off switch from proto.io but i can not get it to work.
as of right now if i have <div id='light'></div>
it will display "On" or "off" depending the state. I need to have it add/remove "checked" to <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="" checked>
switchclick='';
if (vdata == 'Off' ) {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
vdata = 'Off';
} else {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
vdata = 'On';
}
if( SwitchType == 'On/Off') {
// code to be executed if condition is true
$('#'+vlabel).html('<div '+switchclick+' >'+vdata+'</div>');
}
I hope I explained my self and what i am wanting to do, if not please let me know and not down vote me. I am trying. :)
Full script for reference.
function RefreshData()
{
clearInterval($.refreshTimer);
var jurl=$.domoticzurl+"/json.htm?type=devices&plan="+$.roomplan+"&jsoncallback=?";
$.getJSON(jurl,
{
format: "json"
},
function(data) {
if (typeof data.result != 'undefined') {
$.each(data.result, function(i,item){
for( var ii = 0, len = $.PageArray.length; ii < len; ii++ ) {
if( $.PageArray[ii][0] === item.idx ) { // Domoticz idx number
var vtype= $.PageArray[ii][1]; // Domotitcz type (like Temp, Humidity)
var vlabel= $.PageArray[ii][2]; // cell number from HTML layout
var vdesc= $.PageArray[ii][3]; // description
var vattr= $.PageArray[ii][4]; // extra css attributes
var valarm= $.PageArray[ii][5]; // alarm value to turn text to red
var SwitchType= item.SwitchType; // Switch type "Dimmer""On/Off" "ECT"
var vdata= item[vtype]; // current value
//For Dimmers, if a Dimmer is "off it shows off, If on it shows the percent with a %"
if (item.Status == 'Off'){
DimmerLVL = 'Off';
} else {
DimmerLVL = (+item.Level+ '%');
}
// create switchable value when item is switch
switchclick='';
if (vdata == 'Off' ) {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
vdata = 'Off';
} else {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
vdata = 'On';
}
if( SwitchType == 'On/Off') {
// code to be executed if condition is true
$('#'+vlabel).html('<div '+switchclick+' >'+vdata+'</div>');
} else if (SwitchType == 'Dimmer') {
// code to be executed if condition is false
$('#'+vlabel).html('<div>'+DimmerLVL+'</div>');
} else {
// code to be executed if condition is false
$('#'+vlabel).html('<div>123'+vdata+'</div>');
}
// scene alle verlichting uit: http://192.168.0.100:8080/json.htm?type=command¶m=switchscene&idx=2&switchcmd=ON
// if extra css attributes. Make switch not switchable when it is protected.
$('#desc_'+vlabel).html(vdesc);
}
}
});
}
});
$.refreshTimer = setInterval(RefreshData, 8000);
}
$(document).ready(function() {
$.roomplan=2; // define roomplan in Domoticz and create items below.
$.domoticzurl="http://192.168.1.125:8080";
//format: idx, value, label, description, [override css], [alarm value]
$.PageArray = [
['4', 'Status', 'cell6', 'Lamp',],
['2', 'Status', 'cell7', 'TV',],
];
RefreshData();
});
function SwitchToggle(idx, switchcmd)
{
console.log('function called');
$.ajax({
url: "http://192.168.1.125:8080/json.htm?type=command¶m=switchlight" + "&idx=" + idx + "&switchcmd=" + switchcmd + "&level=Level",
async: false,
dataType: 'json',
success: function(){
console.log('SUCCES');
},
error: function(){
console.log('ERROR');
}
});
RefreshData();
}
Upvotes: 0
Views: 1264
Reputation: 87
So I did it the dirty way by adding
<div class="onoffswitch"><input '+switchclick+' type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" '+vdata+' ><label class="onoffswitch-label" for="myonoffswitch"><span class="onoffswitch-inner"></span><span class="onoffswitch-switch"></span> </label></div>
to
$('#'+vlabel).html('');
the whole line of code looks like this
$('#'+vlabel).html('<div class="onoffswitch"><input '+switchclick+' type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" '+vdata+' ><label class="onoffswitch-label" for="myonoffswitch"><span class="onoffswitch-inner"></span><span class="onoffswitch-switch"></span> </label></div>');
I know this is a dirty way to do this but it seems to work for now :(
Thanks to all who try to help :)
Upvotes: 0
Reputation: 3630
Does this work for you?
$('#light').on('click', function() {
var state = $('#light').html();
var $checkbox = $('.onoffswitch-checkbox');
$('#light').html(state == 'Off' ? 'On' : 'Off');
$checkbox.prop('checked', !$checkbox.prop('checked'));
})
#light {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The checkbox:
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="" checked>
<br>
<br>Click the red div, The switch is:
<div id="light">On</div>
Upvotes: 1
Reputation: 1013
I'm not entirely sure if i'm answering this correctly but from what I understand you want to remove the attribute checked? They have the documentation for that at http://api.jquery.com/prop/
For brevity:
Concerning boolean attributes, consider a DOM element defined by the HTML markup , and assume it is in a JavaScript variable named elem:
$( elem ).attr( "checked" ) (1.6.1+) "checked" (String) Will change with checkbox state
Upvotes: 1