Frontaccounting: onchange not working in selectbox fetched using ajax - javascript

var replinks = {'a.repopts_link': function(e) {
e.onclick = function() {
save_focus(this);
set_options(this);
JsHttpRequest.request(this, null);
performCustomization();
return false;
}
},}
Behaviour.register(replinks);
function performCustomization(){
selectbox_selector.onchange = function(){
alert(this.value);
}
}
I am using frontaccounting PHP based application. And I want to perform some task onchange value of select box. But form is being loaded by ajax.
I have tried above code but code in onchange section not working. If I put alert above onchange section so it is working. And I am getting the selectbox selector.
Small help will be very appreciated.

Related

How to set on change as a default on page load jquery?

I have created a on change method for a select box of my project. On selecting particular option it is basically showing and hiding a div which is perfectly working fine. Now, my problem is when first time page is loading this show and hide not working for first default section of form. Can I make this onchange function also working when page load first time.
$('.contact-form').on('change', (e) => {
var selectedId = $(e.currentTarget).val();
var listofforms = $("#discount").data("display-for").split(",");
if (listofforms.indexOf(selectedId) !== -1) {
$("#discount").collapse('show');
}
else {
$("#discount").collapse('hide');
}
});
Here you go with a solution
function changeMethod(selectedId) {
var listofforms = $("#discount").data("display-for").split(",");
if (listofforms.indexOf(selectedId) !== -1) {
$("#discount").collapse('show');
}
else {
$("#discount").collapse('hide');
}
}
changeMethod($('.contact-form').val())
$('.contact-form').on('change', (e) => {
changeMethod($(e.currentTarget).val());
});
You need to move your code outside the change event, so I have kept your existing code within a method changeMethod.
Then call the method from to places
From you change event method
OnLoad of the JS file
Is it possible can I make my on change trigger on page load
Yes, you will just need to change your on change event from e.currentTarget to this as on page load e.currentTarget will be null, but this always points to the current element like:
$('.contact-form').on('change', function() {
var selectedId = $(this).val();
// Your other logic here
});
and to trigger this change event on page load, simply add .change() at last like:
$('.contact-form').on('change', function() {
var selectedId = $(this).val();
// Your other logic here
}).change(); //<---- here

How to get onclick event for jQuery Chosen drodpown listbox?

Hi I am developing one jquery application. I ghave one dropdownbox with jquery choosen.
$(function () {
$(".limitedNumbSelect").chosen();
});
This is my dropdown and binding values from database.
<b>Awarded To:</b> <asp:ListBox ID="ddlvendorss" runat="server" SelectionMode="Multiple" class="limitedNumbSelect"></asp:ListBox>
I am trying to get click event for the above dropdown. As soon as i click on the dropodwn i want to fire a alert before loading any options.
$('#ddlvendorss').click(function (e) {
alert("I am going crazy");
});
In the below code checkedValues arrays contains some values(values present in dropdownlistbox). As soon as i click on the drodpown i ant to hide those values. But below code doesnt work.
$(".chzn-select").chosen().on('chosen:showing_dropdown', function () {
$(".limitedNumbSelect option").each(function () {
var val = $(this).val();
var display = checkedValues.indexOf(val) === -1;
$(this).toggle(display);
$('.limitedNumbSelect option[value=' + display + ']').hide();
$(".limitedNumbSelect").find('option:contains(' + display + ')').remove().end().chosen();
});
});
Above code does not work. May I get some advise on this? Any help would be appreciated. Thank you.
Chosen hides the select element, thus you are not actually clicking the element. However you can use chosen:showing_dropdown event
$(".chzn-select").chosen().on('chosen:showing_dropdown', function() {
alert('No need to go crazy');;
});
Fiddle
If you want to hide options, You can use
$(".chzn-select").chosen().on('chosen:showing_dropdown', function() {
//Find options and hide
$(this).find('option:lt(3)').hide();
//Update chosen
$(this).chosen().trigger("chosen:updated");
});
Fiddle
As per OP's code
$(".chzn-select").chosen().on('chosen:showing_dropdown', function () {
//Get all options
var options = $(this).find('option');
//Show all
options.show();
//Hide based on condtion
options.filter(function () {
return checkedValues.indexOf($(this).val()) === -1;
});
//Update chosen
$(this).chosen().trigger("chosen:updated");
});
instead on using on click, use on change e.g.:
jQuery('#element select').on('change', (function() {
//your code here
}));

Have textbox recognize copied input from button

I'm very new at this and I tried to search, couldn't find answer.
I have several buttons that have a value assigned to them and when I click them, it copies text into another textbox with a submit button underneath that.
I'm having trouble having the textbox recognize that the value was input into the textbox to therefore enable the submit button.
Sample of what I have:
$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
});
$('#textBox').on('input', function () {
if ($(this).val().length>0);
$('#submitBtn').removeAttr('disabled');
$('#submitBtn').removeClass('disabled');
});
Any help is really appreciated.
As can be read in this StackOverflow question, jQuery doesn't fire events when using the .val() method. This means that your code
$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
});
will not fire "input", or "change" events. You could trigger the event yourself:
$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
$('#textBox').trigger('input');
});
Alternatively, you could easily call the function to disable the button from the buttonToCopy as well:
$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
EnableSubmitButton()
});
$('#textBox').on('input', function () {
EnableSubmitButton()
});
function EnableSubmitButton(){
if ($(this).val().length>0) {
$('#submitBtn').removeAttr('disabled');
$('#submitBtn').removeClass('disabled');
}
}

Dynamically added select change events not registering

In my site I have some code as follows: -
$(function() {
$(document).on('change', 'select', function() {
var formID = $(this).attr("id");
if((formID.indexOf("forms[")>-1)){
var newFormID= $(this).val();
var scenarioID = ${testScenarioInstance?.id}
getFormInformation(newFormID, "forms", $(this), scenarioID);
}
});
});
In another part of the page the user can add additional selects via a simple little post and append. The trouble is for any newly added selects this piece of code doesn't trigger, but it works fine for any select that is there when the page loads. Any ideas why this is?
It should work well as you have it now, here I made little example with your same code for the event:
http://jsfiddle.net/2KCY9/
$(document).on('change', 'select', function () {
alert("Works well");
});

Get state of checkbox using javascript

I'm trying to write a javascript with jquery which should be able to pick out and use information on if a checkbox is checked or not. This should happen every time the checkbox(has id 'edit-toggle-me') is clicked. I've written a test function for this with some alert() in it to see if I've succeeded or not.
(function ($) {
"use strict";
$(document).ready(function () {
$('#edit-toggle-me').click(function(){
if ($('#edit-toggle-me').checked()) {
alert('Yup!');
}
else {
alert('Nup!');
}
});
});
})(jQuery);
It perform neither of the alerts so I'm guessing it crashes at $('#edit-toggle-me').checked(). I don't know why though.
I've also tried this:
(function ($) {
$(document).ready(function () {
$('#edit-toggle-me').click(function(){
var elementy = document.getElementById('edit-toggle-me');
var check = elementy.value;
alert(check);
if(elementy.checked()) {
alert('yup');
}
});
});
})(jQuery);
The first alert works, but neither or the last two 'yup' or 'nup'.
And then I also tried this:
(function ($) {
$(document).ready(function () {
$('#edit-toggle-me').click(function(){
var element2 = document.getElementById('edit-toggle-me');
var check = element2.value;
alert(check);
});
});
})(jQuery);
This always return 1. Which I don't understand why either.
Grateful for any hints.
Use .is(':checked'). You can find the documentation HERE.
JSFIDDLE
There is no such method as .checked() in jQuery or in the DOM API. There is simply a .checked property on the element that is true if the element is checked, false otherwise. Try this:
(function ($) {
"use strict";
$(document).ready(function () {
$('#edit-toggle-me').click(function(){
alert( this.checked ? ":)" : ":(" );
});
});
})(jQuery);
See demo: http://jsfiddle.net/scZ3X/
$(function(){
$('#edit-toggle-me').on('change', function(){
if($(this).is(':checked')) {
alert('checked');
}
else {
alert('unchecked');
}
});
});
You should have to use �change� event instead click. Because In some cases click event is not good to use in check box and radio buttons.
Example 1:
If you have a radio button have click event bind. In case your radio button is already true and you clicking in radio button. It�s not change radio button value, But your click event fire every time.
But in case if you use change event. Your event will fire only if your radio button value got change(If it�s already not checked or true)
Example 2:
If you have any label for any radio button or check box. In case you have click event bind in checkbox or radio button. On click of your label your check box or radio button value got change but your click event will not call.
In case of change event. It�ll work as expected on click of label related label too.
Try this:
$('#edit-toggle-me').is(':checked')

Categories