Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:25+00:00 2026-06-14T11:28:25+00:00

Most of user interaction elements associated to a custom JavaScript behavior in Web applications

  • 0

Most of user interaction elements associated to a custom JavaScript behavior in Web applications can be HTML links (a elements) having a meaningful href attribute value, enabling them to be used in non JavaScript-enabled environments:

<a id="profile" href="profile">Profile</a>
<script>
  document.getElementById("profile").onclick = function() {
    return !open(this.href, "_blank", "scrollbars=no,status=no"); // whatever
  };
</script>

But some interaction elements are deeply linked to JavaScript, either because the Web application they are contained in requires JavaScript to run or because they were generated by JavaScript and don’t make any sense when it is not available.

For those, as I want users to be able to interact with them whatever device they are on (i.e. I don’t want to define mouse, keyboard, touch, … interaction by myself on a span element), I see two relevant HTML elements: a and button.

My problem with the a element here is that it defines at least one behavior I don’t want: the ability for the user to open its target anywhere he wants to (e.g. in a new tab), whereas the interaction I want to take place is specific to the current tab.

My problem with the button element here is that, as far as I can tell from the online resources, it is difficult to style reliably on all modern browsers (but I am not sure if it is still the case now).

Some of the facets of this question have already been answered elsewhere, but I can’t find a comprehensive and up-to-date summary: what HTML element would you recommend to use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:28:26+00:00Added an answer on June 14, 2026 at 11:28 am

    If you want an element to semantically be a button without the style issues of a <button> element, or behavior of an <a href> element, then you should use an element with [role="button"]. <span> is commonly used, but pretty much any element could be used.

    <span role="button"></span>
    

    Now, [role="button"] is really just a flag for assistive technology, so some interactions need to be set up to react as a button, but they’re actually quite easy.

    Buttons (such as links and form elements) are typically tabbable. This isn’t always necessary, such as if a keyboard shortcut has been set for it already. If you want the <span> in the tabbing order, just add the [tabindex] attribute:

    <span role="button" tabindex="0"></span>
    

    Now you can tab to the button, but you’d probably still want to trigger the click event when Enter and/or Space is pressed.

    Simply adding an event listener to the button is enough.

    For brevity this example uses jQuery:

    $(document).on('keydown', 'span[role="button"]', function (e) {
        if (e.which === 13 || e.which === 32) {
            $(this).click();
            e.preventDefault();
        }
    });
    

    This uses an event delegation format to provide click support for all spans with [role=”button”], you may want to choose a different selector depending on your situation.

    Now all that’s left is to listen for when the button is clicked:

    Again, jQuery:

    $('.myButtonClass').click(function () {
        ...do stuff...
    });
    

    Now, for other devices, you’re going to want to trigger a click on, say, a touch event. If you’re using jQuery, there are assorted libraries to support turning touch into click and/or tap. If you’re not using jQuery, it’s not a lot of work to listen for touch events.

    I’m not going to provide a code example to handle touch, but that’s because it depends on what the button is supposed to do. In some cases you want to trigger a handler simply by starting a touch on the button (equivalent to mousedown), in other cases you want to trigger the handler if you’ve started and stopped the touch event on the same element (similar to how click works normally).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most web applications depend on some kind of session with the user (for instance,
A user can post 4 times at most within a day. The difficulty is
Can I ask the user WHERE to install the application during install time? Most
I'm working on a program that uses HTML/CSS/Javascript/JQuery for its user interface. One of
I would assume that most User-defined Exceptions are for Business Logic level exceptions, but
Most of my user stories end up with tasks for create code review, carry
Most systems will have a user-defined function (UDF) available. Some will not. i want
What is the most effective to way to make Catalyst::Plugin::Authentication work if the user
Is mysql_real_escape_string sufficient for cleaning user input in most situations? ::EDIT:: I'm thinking mostly
I know $_SERVER['DOCUMENT_ROOT'] returns the document root (most likely something like /home/user/public_html), but is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.