2

i am using while loop to output data in

   <p><?php echo $title;?></p></font></a><button id=show>show()</button>               <button id=hide>hide()</button>

my show hide function is

$("#show").click(function () {
   $("p").show('fast');

});

$("#hide").click(function () {
   $("p").hide('fast');
});

 $("#reset").click(function(){
    location.reload();
});

now when i am clicking show hide only the first show hide loop is working and it shows/hides all the data not just the one i clicked

VolkerK
  • 95,432
  • 20
  • 163
  • 226

4 Answers4

6

Change the code to use this, like so:

$(this).prev('p').show('fast');

You will need to do this in each JQuery .click section.

Edit: Another good point which has been mentioned, you are using an ID for your element which won't allow this to work on more than one. Your new markup should look like:

<p><?php echo $title;?></p></font></a><button class="show">show()</button>    

and the JQuery:

$(".show").click(function () {
   $(this).prev('p').show('fast');

});
webnoob
  • 15,747
  • 13
  • 83
  • 165
1

Welcome to SO. Nice to see that you have formated your first question nicely.

Few things you might want to change.

As you are going through a loop, make sure you use a counter inside the loop and add the counter to the id. This will make the id a unique identifier. Also wrap them inside a div.

 $counter = 0;
 forloop {

    $counter++;
    <div>
    <p><?php echo $title;?></p></font></a><button id="show<?php echo $counter; ?>">show()</button>
    </div>        

 }

So now your id will be unique. Now you can write your jquery in the below way.

$("button").click(function () {
  $(this).attr('id'); //not required but incase you need the id of the button which was clicked.
  $(this).parent().find("p").show('fast');

});

$("button").click(function () {
   $(this).parent().find("p").hide('fast');
});
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29
0

Two things: 1. I think you can only have one element with one id, such as #show. If you want to reference more buttons, you should use a class, such as this: show() (as I understand the buttons are output in a loop, there will be more of them).

Second: inside your javascript code, you do $("p")... - this references all

elements on the page. I think you should use $(this) and start from there, check out this article, it explains a lot: http://remysharp.com/2007/04/12/jquerys-this-demystified/

Piotr Kempa
  • 412
  • 2
  • 6
-1

There are many ways to go at this. Here's one:

First, add the loop number to the IDs (let's say it's $i)

<p id="TITLE_<?php echo $i; ?>" style="display:none;"><?php echo $title;?></p>
<input type="button" class="show" data-number="<?php echo $i; ?>"/>               
<input type="button" class="hide" data-number="<?php echo $i; ?>" />

Your functions will then be:

$(".show").click(function () {
   $("TITLE_" + $(this).data('number')).show('fast');
});

$(".hide").click(function () {
    $("TITLE_" + $(this).data('number')).hide('fast');
});

Of course there are ways to do it via JQUERY without the use of the $i.

Edit: To have the

tags hidden on page load, either use the style=display:none as I have added in the

tag above, or you can use JQuery as follows:

$(document).ready( function() {
    $("p[id^=TITLE_]").hide(); 
// this will retrieve all <p> tags with ID that starts                    
// with TITLE_ and hide them
});
PoloRM
  • 155
  • 3
  • 15
  • Just why .... Why over complicate things especially for someone who is obviously new to this. `this` is the correct thing to use here. – webnoob Oct 29 '12 at 09:16
  • @webnoob Thanks for the downvote. Your answer does not do what he wants I believe. You're hiding the button itself, whereas he wants the button not to be hidden, he just wants the title to get hidden. This is a very simply straightforward way which does not require him to change the structure of his HTML (such as putting the buttons inside the

    tags...)

    – PoloRM Oct 29 '12 at 09:18
  • And correct me if I'm wrong, but your new edited answer makes all the "P" tags hidden/show on click of any of the buttons. – PoloRM Oct 29 '12 at 09:21
  • No, it looks at the last p relative to the button which has been clicked. – webnoob Oct 29 '12 at 09:21
  • Oh .. sigh. My fingers engage before my brain sometimes. I have upvoted your comment (for pointing out my error) but I still have to leave this as a downvote as I think its over complicated when using something as accessible as JQuery which has a wide range of selectors. Thanks for your pointers. – webnoob Oct 29 '12 at 09:26
  • My method is not complicated at all, and at least it works regardless of how he re-arranges his HTML tags, as well as uses a very practical technique in JQuery which he will most probably need in the future. But oh well we all have our opinions. – PoloRM Oct 29 '12 at 09:48
  • Fair point. If you edit your answer in some way I will retract my down vote (vote is locked atm). – webnoob Oct 29 '12 at 10:27
  • i got the point modified my page accordingly but just one slight problem is that i need to display when someone clicks show . show hide is working perfectly fine but initially everything is shown – Abhishek Tripathi Oct 29 '12 at 11:36
  • @AbhishekTripathi I have edited my answer for you. You can either use the display:none in the p tag (which is preferable I think, since the page will load automatically with the

    hidden), or if you want to do it the JQuery way you can do it use the document.ready (but effectively, this will hide the

    tags AFTER the page loads).

    – PoloRM Oct 29 '12 at 12:33