How To Call Different Function on React Nested Div onClick Event? [duplicate] - javascript

This question already has answers here:
How can I prevent event bubbling in nested React components on click?
(10 answers)
Closed 11 months ago.
I have a nested div in react like this:
<div onClick={() => fn_01()}>
<div onClick={() => fn_02()}>
</div>
</div>
I want to call different function when user click those div. But when I click the 1st div it called fn_01() and when I click the 2nd div it also call fn_01(). How can I solve this issue.
I am using functional component in my react project. I also implement Class component and call those method using this., which doesn't work also. I need this work in Functional Component

The solution was to place a call to event.stopPropagation() in your child onClick method. This will prevent the parent from being called.

Related

When are stripe elements ready in the dom? [duplicate]

This question already has an answer here:
How to focus on element that loads after page load
(1 answer)
Closed 4 years ago.
I am using Stripe elements in a Vue component. The elements show nicely in a card, but now I would like to put a v-show on the card when the elements are all 'ready/ mounted'. My question: Is there a way to determine when the elements are completely ready?
I tried something along these lines in an async 'createElement' component method:
this.paymentRequestButton.mount('#payment-request-button')
this.card.mount('#card-element')
this.stripeElementsReady = true
The elements do show but this.stripeElementsReady triggers too early, or can iframe elements not be timed precisely as they are asynchronous 'by nature' or something? BTW I do use await for the actual stripe requests so that should not cause the issue.
Check out this answer
As per the docs you can attach a listener on the element and then call focus when it has mounted.
Not tested code but should look something like this
card.on('ready', function(){
card.focus();
}
)

fastest way to add a click handler to elements being added to the dom [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 4 years ago.
Let us say we are adding several elements to the dom. What is the best way to make sure these elements also have a click handler added to them? For example, if I have a click handler on all elements with the class "canvas-section", and I keep adding "canvas-section" elements, what is the best way to make sure in Jquery that these new "canvas-section" elements also have that click handler added.
Before I used to use the "live" jquery function or "on" but those don't seem to work for elements that are dynamically added or added after the original click listener is added.
You can bind the click handler to a parent object, including document, like this:
$(document).on('click', '.div-to-click', function() {
console.log('hi!');
});
See: https://api.jquery.com/on/

Angular 2 - How to add (click) event to dynamically added div [duplicate]

This question already has answers here:
Angular2 - catch/subscribe to (click) event in dynamically added HTML
(2 answers)
Closed 5 years ago.
I try to append a div into my page via a function in my component like this :
showTile($event: any,id: number){
$($event.target).append('<div (click)="somefunction(id)"></div>'))
}
somefunction(id :number){ console.log(id) }
The div has been appended by somefunction is not handled, in angular js 1.x I used to work with $compile.
You should not use jQuery to affect the DOM tree when your application works with Angular2. There is always a way to avoid this kind of the anti-pattern.
If I understand properly, you want to append the div on every click on the element. Why don't you create another component for this? In template, use *ngIf directive in order to show/hide additional content and bind click event to #Component, which will toggle boolean property. Then in parent component just use this component as many as you want. If these child components are specific and needs information, which are available in parent component, just #Input them. If you want to invoke parent component function, you could use #Output in child and then - invoke parent function.
There you can read a lot about this - https://angular.io/docs/ts/latest/cookbook/component-communication.html
Inside component.html file
<div *ngIf="condition" (click)="dosomething(data)">
<label >Click here </label>
</div>
//You div will appear only if condition is true
Inside component.ts file
dosomething(item){
console.log(item);
//do something here
}

click function after dynamically changing data using .html() [duplicate]

This question already has answers here:
Add click function to dynamically created html tags
(5 answers)
Closed 8 years ago.
I am trying to implement a click function inside a div with some nested children tags such as span, td, etc. However, these nested tags are loaded dynamically, mostly using ajax(). The returned result is displayed using .html(data) function. However, once the data is changed and new tags are added, the old javascript written to detect the clicks no longer work.
I want to know if there is a way to make this work?
An example of what i am talking about can be found here.
You are supposed to attach the event handler on the wrapper element like so:
http://jsfiddle.net/V4Sfw/1/
$("#testing").on("click", "span", function() {
alert("now?");
});
$("#testing").html("<span>How about now?</span>");
You could use live to attach handlers that always work, as long you know the structure of the loaded HTML.

JavaScript - adding a class to a child of a button

I'm trying to work out how to add a class to a child element of a button, specifically to the <i> below:
<button><i class="icon-undo"></i> Undo</button>
Ideally, using plain JavaScript, there'd be a function which can be added to the button (i.e. <button onclick="function()">) which would add the class "icon-spin" to the child <i> of whatever element was clicked (so it could be used in multiple places in the same page).
Maybe the code in this question helps.... posted the answer today itself. Just pass 'this' to the function and you could do wonders to the DOM thereafter
get nearby sibling in javascript

Categories

Resources