Chrome doesn't submit form when using AngularJS directive on submit button - html

I've written an AngularJS directive, shown below, which disables a HTML button on form submit, or when the button is clicked, and replaces the button's inner text. This works perfectly in IE and Firefox but in Chrome the code to modify the button executes successfully however the form won't submit, and no request is made to the server.
An example of the HTML button:
<button submit-button submit-title="Generating Report..."
type="submit" class="btn btn-primary">
<i class="fa fa-database mr-xs"></i>Generate Report
</button>
The AngularJS directive:
(function () {
function submitButton() {
return {
restrict: 'A',
require: '^form',
scope: {
submitTitle: '#'
},
link: function ($scope, $element, $attributes) {
$element.on('click', function () {
var element = $element[0];
element.innerHTML = element.innerHTML
.replace(element.innerText, $attributes.submitTitle);
element.disabled = true;
$element.addClass('disabled');
});
}
};
}
angular.module('adminApp', [])
.directive('submitButton', submitButton);
})();
I've used Fiddler to review the requests and responses and can confirm there is no request made in Chrome when the button is clicked or form submitted. Any help would be appreciated.

Chrome won't submit the form if you in the attached click event handler disable the element.
Use the submit event instead:
$element.on('submit', function() {
// Do stuff
});

Related

select2 not loading in bootstrap5 remote modal the first time. second time works

I recently moved from bootstrap3 to bootstrap5. Select2 in modal stopped working. I know there has been known issue in this which was resolved using
$.fn.modal.Constructor.prototype.enforceFocus = function() {}
Or using dropdownParent option for the select 2 to bind to the modal. But this didn't work for me.
So I am loading content (including modal) via ajax and populate Select2 inside it. When I click the link first time, select2 is invisible altogether. I close the modal and click the link again, it works fine the second time. I tried dame removing tabindex as well as tabindex='false'.
Clicking first time:
closed the modal and click the second time:
What am I missing here.
my Ajax code is :
HTML: <a href='url-to-controller-returning view' class='modal-remote'>Add</a>
$(document).on('click', '.modal-remote', function (e) {
e.preventDefault();
var $modalRemote = $('#modal-remote'),
url = $(this).attr('href');
$.ajax({
url: url,
beforeSend: function (data) {
if (!$modalRemote.length) $modalRemote = $('<div class="modal fade" id="modal-remote" aria-hidden="true"><div class="modal-dialog ' + modalClass + '"><div class="modal-content"></div></div></div>');
$modalRemote.find('.modal-content').html('<div class="modal-header"><h5 id="remoteModalLabel">Loading...</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><div class="modal-remote-indicator"></div></div>');
$modalRemote.modal('show');
},
success: function (data) {
$.fn.modal.Constructor.prototype._enforceFocus = function() {};
var $dom = $(document.createElement('html'));
$dom[0].innerHTML = data;
$dom.find('link').remove();
$modalRemote.find('.modal-header > h5').html($dom.find('title').text());
$modalRemote.find('.modal-body').html($dom.html());
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$modalRemote.find('.modal-header > h5').html('Error');
$modalRemote.find('.modal-body').html(XMLHttpRequest.responseText);
}
});
});

css style is appending only once in directive, i want the style continuously when we clicks button

I am trying to use css style in directive, the style is coming but it is not coming continuously when I am clicking the button.
For example, I have a button and a css style(bouncing style) and also I am having a directive.
If I click that button many times that bouncing style also have to come continuously , but I am not getting the style continuously.
I also used $watch in that also only once the style is coming.
<div class="row">
<button class="btn btn-primary" ng-click="qty=qty-1">-</button>
<span my-directive="qty" class="box " ng-bind="qty">{{qty}}</span>
<button id="target" class="btn btn-primary" ng-click="qty=qty+1">+</button>
angular.module('docsSimpleDirective', []).controller('Controller', ['$scope', function($scope) {
$scope.qty =1; }]).directive('myDirective', function() {
return {
restrict:'A',
link:function(scope,elem,attr){
var btn = document.getElementById('target');
btn.addEventListener('click', moveMe);
function moveMe() {
console.log("coming to move");
elem.addClass('bounce');
}
/* scope.$watch(attr.myDirective, function (newValue, oldValue) {
if (newValue !== oldValue) {
console.log("$watch");
console.log(newValue, oldValue);
elem.addClass('bounce');
}
});*/
}
}; });
Here is my plunker
Can you please solve my problem?
Thanks in advance!
You need to remove class bounce after execution of moveMe function. Can use callback function.
function moveMe() {
console.log("coming to move");
elem.addClass('bounce');
setTimeout(function() {
elem.removeClass('bounce');
}, 1000);
}

Text input is using original typed text instead of selected google place autocomplete text

I'm using the Google Maps JavaScript API v3 to autocomplete a text input in my angular/node app.
The problem I'm running into is that for some reason, my angular function is using the original typed text instead of the selected google place autocomplete text that gets filled into the text input.
Here is what happens (in pictures!):
1) Type in the beginning of the place I'm looking for
2) click on the autocomplete place, which fills the text input
3) The string used in my get calls to Google Places API is the original "siz" from step one instead of the place info from autocomplete, which returns the wrong place
Here is the code that I think is relevant to this problem:
From main.html:
<form class="form-horizontal" role="form">
<div class="input-group">
<input ng-model="businessName" googleplace class="form-control input-lg">
<span class="input-group-btn">
<button ng-click="findGPlace2()" class="btn btn-default" type="button">Find!</button>
</span>
</div><!-- /input-group -->
From Google.js controller (the console.log($scope.businessName); is printing out siz in this example):
$scope.findGPlace2 = function() {
console.log($scope.businessName);
GoogleService.findPlace2($scope.businessName).then(function(data) {
$scope.gPlace2 = data;
// for(var i = 0; i < data.results.length; i++) {
// console.log(data.results[i]);
// }
showGData(data.results[0].place_id);
});
};
From App.js (custom autocomplete directive):
analyticsApp.directive('googleplace', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, model) {
var options = {
types: [],
componentRestrictions: {}
};
scope.gAuto = new google.maps.places.Autocomplete(element[0], options);
google.maps.event.addListener(scope.gPlace, 'place_changed', function() {
scope.$apply(function() {
model.$setViewValue(element.val());
});
});
}
};
});
There must be something wrong with your googleplace directive.
Check this fiddile. It might help you
scope.gAuto = new google.maps.places.Autocomplete(element[0], options);
This should be
scope.gPlace = new google.maps.places.Autocomplete(element[0], options);
I think that should do it.

Redirect a user when box is closed

I have a javascript popup context menu with a button below it with close. That button when pressed close will continue to the next page. If a user dont click the close button and decides to click outside the popup box it will not redirect the user. What function for javascript can i add if a user click outside the box it will redirect them without clicking the close button inside the box. using a osx-modal-content plugin
How its triggered*
<input type='button' name='osx' value='Click Here To Enter The Website!' class='osx demo'/></a>
plugin page (link)
OSX STYLE DIALOG ** OSX STYLE DIALOG ** OSX STYLE DIALOG **
http://www.ericmmartin.com/projects/simplemodal-demos/
This is something that i have for the close function**
close: function (d) {
var self = this; // this = SimpleModal object
d.container.animate(
{top:"-" + (d.container.height() + 20)},
500,
function () {
self.close(); // or $.modal.close();
}
This is how you can do stuff on close:
$("#element-id").modal({
onClose: function () {
window.location.href = "http://stackoverflow.com";
}
});
If you had an element added like:
// Load dialog on click
$('#basic-modal .basic').click(function (e) {
$('#basic-modal-content').modal();
return false;
});
Change that to
// Load dialog on click
$('#basic-modal .basic').click(function (e) {
$('#basic-modal-content').modal({
onClose: function () {
window.location.href = "http://stackoverflow.com";
}
});
return false;
});

malsup jquery form submit on select change

I'm using Malsup's excellent Form plugin to dynamically load search results onto the same page.
It works great with a standard form submit, however I have 2 select elements in my form and would love for the results to update as the select is changed.
My code at the moment is thus:
$(document).ready(function() {
var options = {
target: '#bands-results',
beforeSubmit: showRequest
};
$('#bandsearch').ajaxForm(options);
});
// Show loading message and submit form
function showRequest(formData, jqForm, options) {
$('#bands-results').prepend('<span>Searching</span>');
return true;
}
I haven't seen other examples that do the same.
Help appreciated.
Got it licked with this
$(document).ready(function() {
$("#genre-filter").change(function() {
$("#band_search").submit();
});
// bind to the form's submit event
$('#band_search').ajaxForm({
beforeSubmit: showRequest,
target: '#band_list',
success: function() {
$('#premlim').hide();
}
});
})
function showRequest(formData, jqForm, options) {
return true;
}