Basically if the url has cl=true
, I want to toggle the checkbox
URL: http://localhost:8080/general-setup?cl=true
My code:
//auto toggle clone if cl=true
var url = window.location.href;
if(url.contains('cl=true')) {
$('#existingImageToggler').click();
}
I got a error:
Uncaught TypeError: url.contains is not a function
Try below code snippet,
//auto toggle clone if cl=true
if(window.location.href.contains("cl=true")) {
var inputs = document.getElementsById("existingImageToggler");
inputs[i].checked = !inputs[i].checked;
}
Convert URL to string and taht string to lowercase than check using contains methode
if("http://localhost:8080/general-setup?cl=true".toLowerCase().contains("cl=true")){
//logic
}
Are you looking for something like this?
if(window.location.href.indexOf("cl=true") > -1) {
$('#existingImageToggler').prop('checked', true);
}
this is very basic checking if url contains cl=true
Use indexOf
var txt ='http://localhost:8080/general-setup?cl=true'; if (txt.indexOf('cl=true') != -1) { console.log($('#existingImageToggler').click()); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='existingImageToggler'></div>
To toggle the checkbox, you can use the .prop()
function.
$('#existingImageToggler').prop('checked', window.location.href.indexOf('cl=true') != -1);
The second argument should be a boolean ( true|false
). Instead of making an if-else construction, you can also put the condition in there... saves you some code...
You can do something like this
<input type="checkbox" id="chkid" name="">
<script>
//auto toggle clone if cl=true
var url = window.location.href;
if(url.search('cl=true') != -1) {
$('#chkid').trigger('click');
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.