2

I know there are answers to this question all over, but none of them turned out to be satisfying for me.

Is there any official HTML5 way to handle clickable elements that do not have their hrefs?

What I always do is just this:

<a>Link</a>

They might have ID or classes of course, but it doesn't matter for an example. I make them clickable by scripts and don't put any href in them. Some people find it incorrect.

Another options are:

<a href="#">Link</a>
<a href="javascript:void(0);">Link</a>

I hate this approach to be honest, because it makes href attribute containing a garbage.

Even another approach would be to use another tag:

<div>Link</div>

Is there any standarized way to do it? Or, perhaps, any way without proper href is incorrect, because as we all know, links actually should provide proper hrefs for non-js people.

EDIT

Okay, I see the answers are what I expected. To be clear, I know how to make it work and I'm looking for semantically correct way to do it. If you guys think that whatever passes HTML validation is good HTML, then you are wrong.

unor
  • 92,415
  • 26
  • 211
  • 360
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • use `onclick` on any element "except a" and do what you like. Just be aware search engines wont find those links and will kill SEO – Piotr Kula Aug 27 '15 at 06:59
  • @ppumkin They are not meant to be found, those are mainly popups openers or some script features. – Robo Robok Aug 27 '15 at 07:26
  • @Pepo_rasta Because this is still an anchor - element that triggers something. – Robo Robok Aug 27 '15 at 07:27
  • I'm voting to close this question as off-topic because I think that it should be asked elsewhere, it's not really about a programming problem but more about a [UX](http://ux.stackexchange.com/) one (read their help pages before posting there though). Also, you can read [this article](https://css-tricks.com/snippets/css/give-clickable-elements-a-pointer-cursor/) and the comments which show that the question is not clear on what is the recommended practice about "clickable elements". – Kaiido Aug 27 '15 at 07:55

3 Answers3

2

VALIDITY:

There is no problem with using <a> without href attribute. In HTML5 it is a placeholder hyperlink. And it's completely valid. You can try validating. No errors or warnings.

Example usage:

<a id='mya'>Link</a>
$(function(){
    $('#mya').click( function() {
        alert('Click No href'); 
    });
});

See it running

Semantics and everything else:

Read on here for detailed explanations and reasoning.

Community
  • 1
  • 1
DDan
  • 8,068
  • 5
  • 33
  • 52
0

I hate this approach to be honest, because it makes href attribute containing a garbage.

Certainly, garbage is try to remove href attribute in a link, because is how it works, and Google and other search engines can't make navigable your web. You can make with the style that you like. I give you examples:

Example 1

With data-* attribute:

 <a data-link="http://newurl.com">link</a>
 <script>
     $('a').each(function() {
         $(this).on('click', function(e)  {
              var ds = $(this).dataset();
              window.location.href = ds.link;
         });
     });
  </script>

Example 2

With an array in js and ids in html

 <a id="newurl">link</a>
 <script>
     var aLinks = [];
     aLinks['newurl'] = "http://newurl.com";
     $('a').each(function() {
         $(this).on('click', function(e)  {
              window.location.href = aLinks[$(this).attr('id')];
         });
     });
  </script>

Depends on your preferences, choose what you like.

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
0

JavaScript

function popup1() = {
     alert('Please do this....');
 }

HTML Markup

 <div onclick='popup1'> Do you want to join? </div>

Or use jQuery

HTML Markup

 <div class='popup1'> Do you want to join? </div>

jQuery

 $('.popup1').click(function(){   
    alert('Please do this....');
 });

Or use jQuery even cleaner

Cleaner on HTML but crappier in JS - There is a fine balance you need to find

<div > Do you want to join? </div>
<div > Newsletter? </div>
<div > Cookies? </div>

jQuery

$('div').click(function(){   
       if ($(this).text() == 'Do you want to join?')
           alert('Please do this....');
    if ($(this).text() == 'Newsletter?')
           alert('Please do this....');
    if ($(this).text() == 'Cookies?')
           alert('Please do this....');
     });
Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
  • 1
    This does not answer my question, plus `onclick` attribute is ugly. – Robo Robok Aug 27 '15 at 07:29
  • @ppumkin define events in HTML is a bad practice. – Marcos Pérez Gude Aug 27 '15 at 07:31
  • Really... but they are the biggest contributories to W3C!! It just prooves that having clean HTML means nothing. I have seen horrific page sources and the site works. Normal users dont care what the HTML looks like, just that the site works. Browsers deal with obsolete things as if they were not obsoblete... because people still use them. HTML5 is more like an extension for devs who want to use new things. – Piotr Kula Aug 27 '15 at 08:00
  • Yes but as a developer, you don't want to come back and edit any code you wrote because specs have changed and browsers stop supporting your deprecated habits. Better to stick today with what will be standards tomorrow. – Kaiido Aug 27 '15 at 08:01
  • Browsers dont remove obsolete implementations... unless they security threats. Browsers still have to implement ALL specifications, even if marked obsolete. So that websites made in 1989, will still work today, even nobody changed the site for 20+ years! – Piotr Kula Aug 27 '15 at 08:03
  • http://stackoverflow.com/questions/29320161/are-there-examples-of-deprecated-html-elements-that-lost-support-in-current-brow – Kaiido Aug 27 '15 at 08:09