Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use wildcard ID with jQuery and get wildcard ID

I have 4 IDs

hideshow1
hideshow2
hideshow3
hideshow4

Now when the buttons of those IDs are clicked I want to preform individual actions using those integers on the end. So far I have this:

$(document).ready(function() {
  $("[id^=hideshow]").click(function() {

  });
});

Which is enough to act on the click for each of those buttons, but I need to extract the integer on the end to use it to only act on the one that was clicked. Example if #hideshow1 is clicked

$('#hideshow%').html('Hide');
$('.success%').addClass('test');

Where the % is where the integer would be extracted from the initial hideshow ID.

like image 960
Bobby S Avatar asked Jan 08 '14 09:01

Bobby S


1 Answers

You could always try to extract the id/index like this:

$(document).ready(function() {
  $("[id^=hideshow]").click(function() {
      var index = parseInt($(this).attr("id").replace('hideshow',''), 10);
  });
});
like image 195
A. Tapper Avatar answered Sep 29 '22 00:09

A. Tapper