jQuery UI-dialog- setting Buttons dynamically and pass values to function - javascript

I'm dynamically setting buttons to the jQuery UI dialog like below.
arrButton = {};
var i = 0;
for (i = 0; i < dialog_buttons.length; i++) {
for (var key in dialog_buttons[i]) {
arrButton[key] = {
id: key,
text: dialog_buttons[i][key],
click: function () {
test($('#' + key));
}
};
}
}
function test(value) {
if (value.selector == "#confirm") {
confirmthis();
}
if (value.selector == "#cancel") {
cancelthis();
}
}
$("#" + div.id).dialog({
modal: true,
buttons: arrButton
});
when the button clicked, the key will be the last value from the iteration. Let's say we have keys confirm and cancel then when the button is clicked, the key parameter will be cancel.
So when I click a button from the dialog, I want to know which button was pressed.
Please find the fiddle which duplicates the issue I described above.
http://jsfiddle.net/DOmEl/qNGEw/11/
This contains a nested dialog which in turn trigger when clicked either Yes Or No of first dialog.
When the second dialog appear, both button have no effect.

why you are not using this ?
click: function () {
test($('#' + jQuery(this).attr('id')));
}

Related

How can I dispatch a checkbox click programmatically?

I've tried several javascript functions here and there but it seems that none of them worked for me.
What I'm doing is displaying a tower, that has as many stages as the user wishes. This is basically creating HTML tags using Js for every stage and the last one is a checkbox.
The thing I'd like to do is that when I click on a checkbox, all the other ones that are over this one would check programmatically also. So this is what I did:
let event = new Event('click');
//Here, q is the maximum stage that the user set.
document.addEventListener('click', function(e) {
let readStr = e.target.id;
if (readStr.startsWith("checkboxFan") && e.isTrusted) {
for (let r=parseFloat(readStr.substr(-1))+1; r<=q; r++) {
document.getElementById("checkboxFan"+r).dispatchEvent(event);
}
}
}, false);
And it's finding the IDs well and doing what I want, but the dispatchEvent isn't working as expected. In fact, it's not working at all. Also, the "click()" function works but the isTrusted is triggered, which is not what I want because the checkbox has to be clicked by the user.
Any solutions?
I've written some code based on your description.
https://stackblitz.com/edit/js-poevax?file=index.js
document.addEventListener(
"click",
function(e) {
let readStr = e.target.id;
if (readStr.startsWith("checkboxFan") && e.target.checked) {
for (let r = parseFloat(readStr.substr(-1)) + 1; r <= q; r++) {
document.getElementById("checkboxFan" + r).checked = true;
}
}
},
false
);
You should check this page.
Input Checkbox checked Property
cheers!

Close a bootstrap warning when clicking anywhere on page

Bootstrap Warnings Image I have two different types of bootstraps alerts (warning and danger). Danger alerts are always suppose to be on the page no matter what. Warning alerts happen when user clicks on the dropdown list carriers it displays a bootstrap warning notification. User has to click on 'x' for it to close. I need it to work when user click anywhere on the page or by clicking on the 'x'.
HomeController.cs
case "Carrier":
var carrierid = (from foo in db.Carriers
where foo.ID == warningid
select foo.WarningID).Single();
if (carrierid != null)
{
warning = (from warnings in db.Warnings
where warnings.IsActive == true && warnings.Id == carrierid
select warnings.WarningBody).SingleOrDefault();
if (warning != null)
{
warning = ("<div class=\"alert alert-warning alert-dismissible\" id=\"myWarning\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">×</span></button><strong>" +
warning + "</strong></div>");
}
else
{
warning = "";
}
}
else
{
warning = "";
}
return Json(warning, JsonRequestBehavior.AllowGet);
default:
break;
warningwriter.js
//// warning display script takes a value of warningid and warningcaller
$(document).ready(function () {
var warningid = 0;
var warningcaller = "Universal";
loadWarnings(warningid, warningcaller);
});
$('#Phones').change(function () {
var warningid = $(this).val();
var warningcaller = "Phone";
loadWarnings(warningid, warningcaller);})
$('#Carriers').change(function () {
var warningid = $(this).val();
var warningcaller = "Carrier";
loadWarnings(warningid, warningcaller);})
function loadWarnings(warningid, warningcaller) {
$.getJSON("../Home/LoadWarnings", { warningID: warningid, warningCaller: warningcaller },
function (warning) {
var select = $('#warnings');
select.append(warning);
});
};
As Martin suggested, it's something you need to do in javascript. I haven't tested this, but it would be something like:
$(document).click(function (event) {
$(".alert").hide();
});
This is basically, clicking anywhere on the page will hide any displayed alert.
Since you have two different types of bootstraps alerts (danger and warning). You have to use ".alert-warning" because that is the one you want to get rid of when user did a mouse click anywhere on page. ".alert" is all of the bootstraps alerts, however, if you need to get rid of a certain type you can call the contextual classes(e.g., .alert-success, .alert-info, .alert-warning, and/or .alert-danger. https://v4-alpha.getbootstrap.com/components/alerts/
$(document).click(function (event) {
$(".alert-warning").hide();
});
$(document).ready(function () {
$("#myWarning").click(function () {
$(".alert").alert("close");
});
});
By doing this, u are making two things wrong:
You are binding the click event to an element, that possibly
doesnt exist when the page is loaded.
You are binding the click
event to a restricted element. This means that the alert wont be
closed when u click anywhere on the page. In this case, only clicks on #myWarning will close the alert.
Finally, you should use what #Bryan already posted :)
Edit:
Assuming that u have a set of alerts that u always want to close on page load, add to this elements a way to identify them, for example a class "close-on-screenclick"
$(document).click(function () {
$(".close-on-screenclick.alert").alert("close");
});
.This should close those elements whenever a click is made on the screen

Jquery - Identify if radio button value has not changed

Is it possible to identify if the value of radio button has not changed?
Currently I am trying to change the confirmation message of submit button on button changed, and do not want any message if the value has not changed. I have something like this now:
$('input[type="radio"]').change(function() {
var selected = $('input:checked[type="radio"]').val();
if(selected == 'true') {
$("#submit_button").data("confirm", "foo");
} else if(selected == 'false') {
$('#fee').hide();
$("#submit_button").data("confirm", "bar");
}
This will change confirm message to foo if button selected is true, and bar if button selected is false. However, what if I want to return nothing (no message), if radio button by default is true, and selected is true?
You can start a variable outside the event:
var radioChanged = 0;
And, in your event increase it:
$(':radio').change(function() {
radioChanged += 1;
// your code ...
});
Then, later on:
if (radioChanged > 0) {
alert('Change function occurred ' + radioChanged + ' times.');
} else {
alert('Radio button not changed.');
}
As i understand your expected behaviour, check if any radio has no more its default checked value:
$('form').on('submit', function() {
var anyRadioChanged = !!$(this).find('input[type="radio"]').filter(function() {
return $(this).is(':checked') != this.defaultChecked;
}).length; // '!!' to get boolean but it doesn't really matter here
if(anyRadioChanged) {
// show message(???)
}
})
you can hide message element just adding display: none to it or use jquery hide method
$('#someElementId').hide();
or
$('#someElementId').css("display","none")

How to click buttons after creating with jQuery?

I have 10 buttons that are created when click button "Create".
How to also click all those buttons when i click "Create" ?
function a() {
selectAll();
jQuery(selectAllValues());
};
function selectAllValues() {
for(var i = 0; i < array.length; i++) {
document.getElementById("select" + array[i]).click();
}
};
The problem is that the buttons are created but not clicked.
Just use Jquery like this :
$("#select" + array[i]).click();
Or without loop : http://api.jquery.com/attribute-starts-with-selector/
//Will execute the click event on all element where the id begins by "select"
$('[id^="select"]').click();
Why do you want to click them all? Are these actually checkboxes that you are trying to mark as "Checked"?
//If checkboxes then try
$('#select'+array[i]).prop('checked', true);
if these are truly buttons then why wouldn't you manually initialize the "onClick" function that they would initialize?
for(var i = 0; i < array.length; i++) {
//call the onClick function yourself here
}
Please elaborate
EDIT:
function selectAllValues() {
for(var i = 0; i < array.length; i++) {
$('#select'+array[i]).prop('checked', true);
//code to create tables
//loop through tables
$('tableselector').each(function(key,value){
//check the checkboxes within the tables
$(this).find('input[type="checkbox"]').prop('checked', true);
});
}
};

Compare onclick action of two html button using javascript

I have this two HTML Form buttons with an onclick action associated to each one.
<input type=button name=sel value="Select all" onclick="alert('Error!');">
<input type=button name=desel value="Deselect all" onclick="alert('Error!');">
Unfortunately this action changes from time to time. It can be
onclick="";>
or
onclick="alert('Error!');"
or
onclick="checkAll('stato_nave');"
I'm trying to write some javascript code that verifies what is the function invoked and change it if needed:
var button=document.getElementsByName('sel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
if( button.getAttribute("onclick") != "checkAll('stato_nave');" &&
button.getAttribute("onclick") != ""){
//modify button
document.getElementsByName('sel')[0].setAttribute("onclick","set(1)");
document.getElementsByName('desel')[0].setAttribute("onclick","set(0)");
} //set(1) and set(0) being two irrelevant function
Unfortunately none of this work.
Going back some steps I noticed that
alert( document.getElementsByName('sel')[0].onclick);
does not output the onclick content, as I expected, but outputs:
function onclick(event) {
alert("Error!");
}
So i guess that the comparisons fails for this reason, I cannot compare a function with a string.
Does anyone has a guess on how to distinguish which function is associated to the onclick attribute?
This works
http://jsfiddle.net/mplungjan/HzvEh/
var button=document.getElementsByName('desel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
var click = button.getAttribute("onclick");
if (click.indexOf('error') ) {
document.getElementsByName('sel')[0].onclick=function() {setIt(1)};
document.getElementsByName('desel')[0].onclick=function() {setIt(0)};
}
function setIt(num) { alert(num)}
But why not move the onclick to a script
window.onload=function() {
var button1 = document.getElementsByName('sel')[0];
var button2 = document.getElementsByName('desel')[0];
if (somereason && someotherreason) {
button1.onclick=function() {
sel(1);
}
button2.onclick=function() {
sel(0);
}
}
else if (somereason) {
button1.onclick=function() {
alert("Error");
}
}
else if (someotherreason) {
button1.onclick=function() {
checkAll('stato_nave')
}
}
}
Try casting the onclick attribute to a string. Then you can at least check the index of checkAll and whether it is empty. After that you can bind those input elements to the new onclick functions easily.
var sel = document.getElementsByName('sel')[0];
var desel = document.getElementsByName('desel')[0];
var onclick = sel.getAttribute("onclick").toString();
if (onclick.indexOf("checkAll") == -1 && onclick != "") {
sel.onclick = function() { set(1) };
desel.onclick = function() { set(0) };
}
function set(number)
{
alert("worked! : " + number);
}
working example: http://jsfiddle.net/fAJ6v/1/
working example when there is a checkAll method: http://jsfiddle.net/fAJ6v/3/

Categories

Resources