Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery onClick preventDefault

Tags:

jquery

I have a table of which I want to suppress the link clicks, because I need the links for other functionality.

The structure of the table is something like this:

<table>
<tr><th>Day</th><th>Event</th>
<tr class="DAY" id="DAY_0"><td>1-8-2013</td><td><a href="?tab=tabCalendar&dayEvent=DAY_0">Add Event</a></td></tr>
<tr class="DAY" id="DAY_1"><td>2-8-2013</td><td><a href="?tab=tabCalendar&dayEvent=DAY_1">Add Event</a></td></tr>
</table

my jquery code to try and block the from refreshing the page, and showing the id is this

<script>
  $("a").click(
      function(event) {
        event.preventDefault();
        alert('Picked: '+ event.target.id.slice(4) );
      }
    );
  </script>

I have also tried the following

$(".DAY").click(function(){//to catch the class DAY.click()

and even

$("[id^=DAY]").click(function(){//to catch the id DAY*.click

however, none of these function did anything.

The versions I use are

jquery-1.9.1.js
jquery-ui-1.10.3.custom.js
like image 741
ShadowFlame Avatar asked Aug 06 '13 12:08

ShadowFlame


1 Answers

Simplest approach:

<script>
   $(".DAY").click(
      function(event) {
         event.preventDefault();
         alert('Picked: '+ $(this).attr('id').slice(4));
      }
   );
</script>
like image 160
Rushil Sablania Avatar answered Sep 28 '22 22:09

Rushil Sablania