David Biga
David Biga

Reputation: 2801

Issue with $(this) in JQuery

Hey guys I have the following:

$(".views").click(function() {
    $(this).(".views").show();

});

$(".closeviews").click(function () {
    $(this).(".closeviews").hide();

});

This will open up a list based on which view list they want to look at, for some reason it's telling me Uncaught SyntaxError: Unexpected token (

Once I remove the (this). it goes away, so I am kinda confused as to why it is telling me that.

EDIT:

I changed to this:

$(".views").click(function() {
    $(this).find(".views").show()

});

$(".closeviews").click(function () {
    $(this).find(".closeviews").hide()

});

Does nothing, if I go to the view list above it opens this one and that one.

UDATE:

HTML:

The one I am trying to open with the above script -

<input type='button' value='View Your Employees' class='views' name='views' /> 
<input type='button' value='Close' class='closeviews' name='closeviews' />

The one I click above this one, opens the above plus this one:

Script:

$(".notempl").click(function () {
    $(".notempltable").show();

});

$(".closenotempl").click(function () {
    $(".notempltable").hide();

});

HTML:

<input type='button' value='View Employees' class='notempl' name='notempl' /> 
<input type='button' value='Close' class='closenotempl' name='closenotempl' />

UPDATE:

Hey guys thanks for all the help, got it sorted out. I actually was telling the wrong thing to show and hide. Each list is populated by a PDO statement and a table so I needed to show the table, hide the table not the button.

Thanks guys :)

Upvotes: 2

Views: 108

Answers (4)

nbrooks
nbrooks

Reputation: 18233

Demo

HTML:

<ul class='viewstable'>
  <li>Sample</li>
  <li>Sample 2</li>
</ul>

<input type='button' value='View Your Employees' class='views' name='views' /> 
<input type='button' value='Close' class='closeviews' name='closeviews' />

JS:

$(function () {
  $(".views").click(function () {
    $('.viewstable').show();
  });

  $(".closeviews").click(function () {
    $('.viewstable').hide();
  });
});

Upvotes: 1

insomiac
insomiac

Reputation: 5664

Why do you want to find the same class. As you can just do $(this) to access the class and its done:

$(".views").click(function() {
    $(".closeviews").show(); //Show close 
});

$(".closeviews").click(function () {
    $(".views").hide(); // Hide view
});

Upvotes: 1

Mihai P.
Mihai P.

Reputation: 9367

It should be

$(this).hide();
$(this).show();

If you remove the $(this) then it will hide and show all the elements with that class. If you click on any of them then all of them will be hidden. I do not believe you want that.

Upvotes: 8

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

I think you mean $(this).find(".views").show()

Upvotes: 5

Related Questions