jQuery click function exclude children except for one - javascript

I have this piece of HTML:
<div class="pop-up rooster-toevoegen">
<div class="pop-up-container">
<div class="pop-up-header clearfix">
<div class="pop-up-title">
Rooster toevoegen
</div>
<div class="sprite close"></div>
</div>
<div class="pop-up-content clearfix">
<form id="rooster-toevoegen-form" class="form rooster-toevoegen-form">
<div class="afdeling-container">
</div>
<div class="date-container">
</div>
<div class="button-container clearfix">
<button value="" name="rooster-toevoegen-button" class="rooster-toevoegen-button button-green">Toevoegen</button>
</div>
</form>
</div>
</div>
</div>
Now I want a click function on .rooster-toevoegen and exclude all children from this click function EXCEPT the button. Also the button has already a function(submitting the form), this must stay the buttons event handler.
CONTEXT:
This is a pop-up with a form inside. When the user clicks next to the pop-up the pop-up has to close. Not when clicking on the pop-up which happens when I don't exclude the children from the click event. BUT when the user clicks on the button the form has to submit. So the button should not be excluded from the click and perform his own action.
How do I do this?

You can prevent from bubbling to all elements using e.preventDefault(), and manually trigger click on button.
Better way is to bind one more click event only to button.

Related

Change event being called on click

I am making a chrome extension and the popup has a single input element to which I'm programmatically assigning an on-change event listener.
$(".search").change(function() {
console.log($(this).val());
});
Here is the html
<div class="title">extension</div>
<div class="animated-page-container">
<div class="stats">
<h1></h1>
</div>
<div class="website">
<div class="search-container">
<div class="search-box">
<div class="container">
<div class="search-icon"></div>
</div>
<input class="search" id="search" />
<div class="icon-container">
<div class="add"></div>
<h1 class="tooltip">Add to list</h1>
</div>
</div>
</div>
<div class="website-list-container">
<div class="website-list"></div>
</div>
</div>
</div>
I have a search icon with class search-icon next to it which has no event listeners attached to it.
The onchange event never gets fired when the input value changes. Instead when i click on the search icon it fires the onchange event. Chrome dev tools doesn't even show any listener being attached to the search icon. I have tried replacing the class of search with an id and attaching a listener to it, but in any case the behaviour remains the same.
The onchange event is always fired when the element loses focus, after the content has been changed. If you're looking for an event that fires immediately after each input, try the oninput event like so:
$(".search").on("input", function() {
console.log($(this).val());
});

Modal disappears due to mouseup event (highlight input field in modal and release mouse outside modal)

An interesting bug where you have a modal with form input fields in it. You then highlight the text in one of the input fields, so the mousedown event occurs inside the modal, but the mouseup event occurs outside the modal.
Chrome 73 interprets the mouseup event outside the modal as a click, and closes the modal.
Here's the original modal code:
<div class="modal-wrapper" (click)="close()">
<div class="modal-background"></div>
<div class="modal-container" (click)="stopEvent($event)">
<!-- modal content here -->
</div>
</div>
The solution is to change the click event that closes the modal to a mousedown event instead:
<div class="modal-wrapper" (mousedown)="close()">
<div class="modal-background"></div>
<div class="modal-container" (mousedown)="stopEvent($event)">
<!-- modal content here -->
</div>
</div>

<autofocus> unable to get focus repeatedly until the page is refreshed

I am designing a footnote at the bottom of an article to annotate following up status;
<article class="col-md-12">
</article>
<div class="col-md-12 footnote">
add a footnote
</div>
When the "add a footnote" link is hidden after being clicked and prompt the "footnote form" which I set autofocus which textarea. The "autofucus" works properly.
$('.footnote-link a').on('click', function (e) {
e.preventDefault();
...
var $footnoteForm = $(`
<form class="article-footnote-form footnote-form" style="margin-top:10px" >
<div class="form-group row">
<div class="col-md-9">
<textarea class="form-control" name="footnote" rows="3" autofocus></textarea>
</div>
<div class="col-md-3">
<button class="btn btn-primary" type="submit" id="articleFootnoteBtn">Add Annotation</button>
</div>
</div>
</form>`);
$articleFootnoteLink.parent().hide();
$articleFootnoteLink.parent().after($footnoteForm);
The form is submitted to server using Ajax then I cleared the forms by
$footnotesTop.find(".footnote-form").html(""). Now the page displays the article and the newly added footnote.
However, if I click the "add footnote" again, it unable to get focus automatically as it does at first time until I refresh the page to click.
How could I get the form focus during the second click event.
You need to use a delegated eventHandler. The js code $('.footnote-link a').on('click', function (e) { is only applied to the elements jQuery finds using that selector when the page is loaded.
Try $( document ).on('click', '.footnote-link a', function (e) {. You can use a tighter selector rather than document - it seems like .footnote would work based on what I see.
jQuery delegated events tutorial

How do I change a variable in a child element that has ng-click in the parent element.

Consider this code:
<section class="page-section about-us" scroll-bookmark="about-us" ng-click="activeSection=true" ng-init="activeSection=false">
<div class="page-content sub-content active-section">{{activeSection}}
<div class="page-border">
<a href="#" class="close-section"><img src="public/images/go-back-icon.png" />
<div class="back-button" ng-click="activeSection=false">CLOSE</div>
</a>
</div>
</div>
</section>
I have an ng-click in the that element which changes the value of 'activeSection' to true. Inside of it, I have another button that can switch this back to it's initial value (false).
In the actual app, it would show or hide this child button based on a class added to the element,just to give you a little background what I'm trying to achieve.
When I click on the element, it does as I expect it to be: switch the value to 'true'. But when I click on the .back-button element with the other ng-click, it fails to register the changed value.
Why is that?
They're both inside the same controller, btw. If there's a solution that doesn't involve creating a new controller, it would be better.
If you click on your back button, activeSection will be false but then your event will be propagated to its parent so the ng-click of Section will be executed too and activeSection will be true again.
In order to make your code work, you should stop the propagation of the ng-click event after changing the value of your variable in your back-button.
Your code would look like this:
<section class="page-section about-us" scroll-bookmark="about-us" ng-click="activeSection=true" ng-init="activeSection=false">
<div class="page-content sub-content active-section">{{activeSection}}
<div class="page-border">
<a href="#" class="close-section"><img src="public/images/go-back-icon.png" />
<div class="back-button" ng-click="activeSection=false; $event.stopPropagation();">CLOSE</div>
</a>
</div>
</div>
</section>
What you are doing wrong is that you are putting the close button inside the element which have already ng-click, that's why when you are clicking the close button, it executes the parent ng-click and stop propagation for all other click events happening simultaneously.
So, the possible solution is making another super parent of the elements and taking the close button out of the element which is making it visible when clicked and adding a ng-show directive to the close button.
Checkout the following snippet
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<section class="page-section about-us" scroll-bookmark="about-us" ng-init="activeSection=false">
<div ng-click="activeSection=true" class="page-content sub-content active-section">{{activeSection}}
<div class="page-border">
<a href="#" class="close-section"><img src="public/images/go-back-icon.png" />
</a>
</div>
</div>
<div ng-show="activeSection" class="back-button" ng-click="activeSection=false">CLOSE</div>
</section>
</div>

How to call reset on variety of inputs from OUTSIDE the form

I have a form with various kinds of inputs, text, drop downs, radial buttons, sliders, and checkboxes. My "Reset" button is outside of the form (above the form). Is there a way to click the button and set all of the inputs back to default?
I have a Twitter Bootstrap accordion, with the reset button on the accordion header. The form is below inside of the accordion. I want to be able to clear the form whether the accordion is expanded or collapsed.
Example:
Accordion heading (Expanded) Reset
input fields
Accordion heading (collapsed) Reset
My reset is in my html like so:
<a id="icon-Reset" href="#form" class="btn-mini" type="reset"><i class="icon-refresh" title="Reset" ></i></a>
and I was trying to do this in my javascript file:
#icon-Reset.click(function(){
$('#form').get(0).reset();
});
but I cannot get anything to work.
To be more clear, here is the top of the html that has the above html line in it:
<div id="side-bar-container">
<div id="sideBar">
<div class="accordion" id="mainAccordion">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse"
data-parent="#mainAccordion" data-target="#collapseOne"> <i
class="icon-chevron"></i>Accordion Heading1
</a>
<a id="icon-Reset" href="#form" class="btn-mini" type="reset"><i class="icon-refresh" title="Reset" ></i></a>
</div>
<div id="collapse" class="accordion-body collapse in">
<div class="accordion-inner scrollable">
<div id="Accordion1">
<form id="form"class="form-search form-horizontal">
<fieldset>
I am VERY new to Javascript, HTML, etc, so please be detailed...Thanks!!
Your approach was correct, but you have to attach form reset action to the click event of your button. It can be any control, assuming it's a span with id "reset"
<span id="reset">Reset</span>
You can make clicking on it reset form with id "form" like this:
$('#reset').click(function() {
$("#form")[0].reset();
})
This code attaches action to span's "onclick" event and inside of action gets the form and resets it. Here's a small demo - fill the form and then click Reset:
http://jsfiddle.net/29ewk/

Categories