user1009749
user1009749

Reputation: 63

Show/Hide divs using Jquery

I am having issues showing one div then hiding the previous div.

var newElem = '#ID' + numVariable;
 $('.elemPropGlobal').hide();

 $(newElem).click(function (){
   $('.elemPropGlobal').hide();

   $($(this).attr('id') + 'Properties').show();

}

Divs that I click on

<div class="something" id="ID1"  > some data</div>
<div class="something" id="ID2"  > some data</div>

Divs that should show and hide

<div class="elemPropGlobal" id="ID1Properties"  > 

<div class="elemPropGlobal" id="ID2Properties"  > 

Upvotes: 0

Views: 161

Answers (3)

nicosantangelo
nicosantangelo

Reputation: 13736

I don't know if it is an oversimplification, but you're not setting the click event for both divs and you're forgetting the hash in the second selector:

$(newElem).click(function (){}); //newElem is #ID1 or #ID2

you could do

    $(".something").click(function (){
       $('.elemPropGlobal').hide();

      $("#" + $(this).attr('id') + 'Properties').show();  //Remember the hash!
    });

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

Currently you're looking for elements that are named ID1Properties etc. You need a # prefix to search by id!

$("#" + $(this).attr('id') + 'Properties').show();

Upvotes: 0

RightSaidFred
RightSaidFred

Reputation: 11327

You forgot the #.

$('#' + this.id + 'Properties').show();

Upvotes: 4

Related Questions