how js know which button is clicked?

javascript click button by id
how to know which button is clicked in jquery
onclick javascript
how to check if a button is clicked in javascript
get button value javascript
javascript button onclick array
javascript click button programmatically
if element is clicked javascript

I'm new to React. So when I create a new React app, for example, a component that render some buttons like

//index.html

<button class="m-2 btn btn-block btn-secondary">Creat</button>
<button class="m-2 btn btn-block btn-secondary">Save</button>
<button class="m-2 btn btn-block btn-secondary">Delete</button>

I know there is a bundle.js behind the scene, but for all the button html tags, there is no "id" attribute to differentiate each button, so when I click the "save" button, how does bundle.js know which button I have clicked?


Each of your buttons will need to be connected to some kind of action using onClick or onSubmit (if in a form). Here's an example of an alert box that fires a mutation function to delete an item if the viewer presses ok...

  <button class="m-2 btn btn-block btn-secondary" onClick={e => {
    if (window.confirm("Are you sure you wish to delete this?"))
      onSubmit(e, removeTagFromDeck);
     }}>Delete</button>

If you need id's to further refine your CSS, you can simply add id='whatever' to the button.

How to get ID of button user just clicked?, Javascript queries related to “how to detect a button click in javascript”. check if button is clicked js � button clicked or not in js � how to detect a� After clicking on the button: Get the ID of clicked button using jQuery. jQuery on() Method: This method adds one or more event handlers for the selected elements and child elements. Syntax: $(selector).on(event, childSelector, data, function, map) Parameters: event: This parameter is required. It specifies one or more event(s) or namespaces to attach to the selected elements.


React uses virtual DOM, the code changes you make, changes the virtual DOM not the real one. When an event such as onclick happens or user typing into an input, React compares the state of the real DOM and virtual DOM, if they are the same nothing happens, if they are different React will update the real DOM to match the virtual DOM.

Add onclick listener to your buttons and pass an argument to the onclick function like below.

onButtonClick = (btn) => {
  console.log(btn)
}

<button onClick={() => this.onButtonClick('create')} class="m-2 btn btn-block btn-secondary">Creat</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<div id="root"></div>

<script type="text/babel">

class App extends React.Component {
  
  constructor() {
    super();
    this.state = {
      result: '',
      isLoading: false
    };
  }
  
  
  
  onButtonClick = (btn) => {
  console.log(btn)
  }
  

  

  render() {

    return (
      <div>
      <button onClick={() => this.onButtonClick('create')} class="m-2 btn btn-block btn-secondary">Creat</button>
      <button onClick={() => this.onButtonClick('save')} class="m-2 btn btn-block btn-secondary">Save</button>
      <button onClick={() => this.onButtonClick('delete')} class="m-2 btn btn-block btn-secondary">Delete</button>
      </div>
    );
  }
}

ReactDOM.render(
    <App />,
    document.getElementById('root')
);
</script>

How to get the ID of the clicked button using JavaScript / jQuery , With a library under the belt, events like click are a no brainer, with vanilla JavaScript things aren't chewed ready as much. I have the following HTML code (I can't change it) and I want to do a javascript code to determine which button of those from A to Z was clicked. Any idea how I can do this? Here is what I tried, but I always get the result "alphabet".


There is a hidden event listeners that is registered to the DOM element. If you use Chrome development tools, you can inspect the button element and you will see in Event Listeners tab that there are part of code which listen to those click events.

There is a addEventListener method which can add event listener to DOM element. https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Initially, the HTML body is empty, when React create elements through JavaScript, it have reference to those elements and thus can call addEventListener on those elements.

Here is an example of how the internal of React may work.

var b1 = document.createElement("button");
var b1text = document.createTextNode("Create");
b1.appendChild(b1text);
b1.addEventListener("click", function() {
	alert("Create");
});

var b2 = document.createElement("button");
var b2text = document.createTextNode("Delete");
b2.appendChild(b2text);
b2.addEventListener("click", function() {
	alert("Delete");
});


var element = document.getElementById("root");
element.appendChild(b1);
element.appendChild(b2);
<div id="root">
</div>

how to detect a button click in javascript Code Example, Depending on the browser, event.button or event.which properties of the event object are used Duration: 11:32 Posted: Jan 20, 2015 Define all of your elements, and then in your JavaScript section, define the various actions that should be performed on those elements. When an event handler is called, it’s called within the context of the element that was clicked on. So, the identifier this will refer to the DOM element that you clicked on.


Detect click with pure JavaScript – clubmate.fi, You still don't know which initiated the click. If the calling function is the button, you can try the this keyword to reference the caller and get the id� The click event occurs when an element is clicked. The click() method triggers the click event, or attaches a function to run when a click event occurs.


JavaScript to detect which mouse button is clicked, Now in query we can declare a variable and set the value to true on the click event of the button. var buttonclicked;. $("#testbtn").click(function(){. Define all of your elements, and then in your JavaScript section, define the various actions that should be performed on those elements. When an event handler is called, it's called within the context of the element that was clicked on. So, the identifier this will refer to the DOM element that you clicked on.


Javascript If Statement for different button clicked?, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� // Get the button, and when the user clicks on it, execute myFunction document.getElementById("myBtn").onclick = function() {myFunction()}; /* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */