0

This is the structure of my jsx.

<a href="/some/link/" title="some time">
  <div>
    Some big Enough Content
  </div>
  <button className="card-detail__link right-price__link" onClick={this.handleCheckRightPriceClick}>
     Check Right Price
  </button>
</a>

a tag wraps around div tag which has enough content to take considerable space and a button tag which has some onClick event listener attached to it. The Desirable behavior is that on the click of anywhere inside div tag, the link in a tag should be opened but if I click on button, only its click handler should be called and the link should not be opened.

To achieve this behavior, I called event.stopPropagation() inside the button's click handler, but the link still opened. Then I removed call to stopPropagation, and called preventDefault, which to my utter surprise worked.

Now according to this documentation, the default type of button is submit, which if present inside a form tag will submit the data to server. But in my jsx, there are no form tags.

Handler Implementation:

handleCheckRightPriceClick = event => {
  // Even if I remove the call to stopPropagation, it doesn't effect the output
  event.stopPropagation();
  // IF I remove call to preventDefault, the link is opened.
  event.preventDefault();
  // does some stuff
};

My Thinking was that calling stopPropagation should have worked instead of preventDefault, as it should stop the event bubbling and the event never reaches the anchor tag

Community
  • 1
  • 1
kumarmo2
  • 1,381
  • 1
  • 20
  • 35
  • 1
    I tried it quickly in my IDE and it complained about the button being nested in an anchor tag. Seems it is invalid to do so. https://stackoverflow.com/questions/6393827/can-i-nest-a-button-element-inside-an-a-using-html5 – Bart Hofland Jul 17 '19 at 07:55
  • I actually referred the same answer. still I had some doubts. Thanks for the confirmation. Also can you tell me what IDE you are using ? @BartHofland – kumarmo2 Jul 17 '19 at 10:40
  • 1
    Regarding the IDE... Sure. :) I use the full-blown version of Microsoft Visual Studio 2019; a Community Edition at home and a Profesional Edition at work. It's quite big (it consumes multiple gigabytes of disk space), but I have been using Visual Studio for over 15 years now (so I am quite used to it) and I also work heavily on various .NET projects as well. – Bart Hofland Jul 17 '19 at 12:14
  • 1
    There are a lot of other editors as well, of course. I have been using JetBrains WebStorm for about a year, but its settings management was a little too complicated to my taste. I tried VS Code, but I disliked it immediately, because its UI is too restrictive for me; it is not possible to "undock" child windows from the main window and move them to my second monitor, for example. – Bart Hofland Jul 17 '19 at 12:14
  • A colleague of mine uses Sublime Edit for everything. That editor is also highly configurable (using plugins), but I couldn't get used to its touch and feel... I think that in the end it's all just a matter of personal preference and personal requirements. – Bart Hofland Jul 17 '19 at 12:14

0 Answers0