9

I have a foreach loop in php like below.

<?php
    $i = 0;
    foreach ($query as $value) { ?>
    <button id="show[<?php echo $i;?>]" class="btn btn-success" type="button">View</button>
    <div id="blah[<?php echo $i;?>]">Joel</div>
<?php 
    $i++ 
 } ?>

Now this loop work fine and I am getting id for each buttons and divs with unique id. But I want to add a jquery click function like below.

$("#show").click(function(){
$("blah").hide();
});

But since it is inside a loop and have different id's with them how to add this jquery function for each buttons?

3
  • add attribute onclick to the html element Commented Jul 18, 2014 at 10:13
  • 2
    You can do it using class instrad of id and then using this on the jQuery code. Commented Jul 18, 2014 at 10:14
  • but remember the class is not used by other elements, otherwise it will call click on those also Commented Jul 18, 2014 at 10:16

6 Answers 6

11
$(".btn").click(function(){
 var id = $(this).attr('id');
 var n = id.replace("show",'');
 $("#blah"+n).hide();
});

Also replace your buttons with this code

<button id="show-<?php echo $i;?>" class="btn btn-success" type="button">Clear</button>
<div id="blah-<?php echo $i;?>">Joel</div>
Sign up to request clarification or add additional context in comments.

8 Comments

this is a good idea also (no idid not downvote), i think the downvote came from the strings [], just a little tweak and this should be ok too
this is the only answer i upvoted before adding my own, sorry kevin i'm just not a fan of making divs children of buttons ...
@ user3702775 : Your login is correct. But unfortunately it doesn't work in this condition. Can you please check
Is there any console error ? Also what do you mean by login ?
@Joel Says: Also the div with id blah is supposed to be inside button tag or outside button tag. You have not closed the button tag anywhere.
|
3

for php:

<?php foreach ($query as $key => $value) {?>
<button class="btn btn-success blah-toggler" type="button" data-target="blah-<?php echo $key ?>">
<div id="blah-<?php echo $key ?>">Joel</div>
<?php }?>

then in jquery:

$(".blah-toggler").on("click", function(){
    var t = $(this);
    $('#' + t.data('target')).hide();
});

Comments

2

Use a class

<button id="show[<?php echo $i;?>]" class="btn btn-success mybutton" type="button">

Then

$(".mybutton").click(function(){
    $("blah").hide();
});

Comments

2

Alternatively you could also use a class. Then the usual:

<?php $i = 0; ?>
<?php foreach ($query as $value): ?>
    <button class="btn btn-success toggle" type="button">
        <div id="blah[<?php echo $i;?>]">Joel</div>
    </button> <!-- missed a closing button tag -->
<?php $i++; ?>
<?php endforeach; ?>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    $('.toggle').on('click', function(){
        $(this).children('div').hide();
    });

});
</script>

Comments

2

u should use following

PHP:

<?php
    $i = 0;
    foreach ($query as $value) { ?>
    <button id="show[<?php echo $i;?>]" class="btn btn-success clickEvent" type="button">
    <div id="blah[<?php echo $i;?>]">Joel</div>
<?php 
    $i++ 
 } ?>

Here you are adding class="clickEvent" for the buttons . And At the end u will have jScript file with following content .which will take care what should happen , when the button with that class is clicked.

jQuery:

$(".clickEvent").click(function(){
$(this).next('div').hide();
});

4 Comments

is blah a tag for you or an id ?
@vortex , its sample , The main logic OP asked for is important. Can u check what op asked ? What should happen after clicking is depending on OP.
yeah i read the question. still looks better after editing :)
@jQueryAngryBird : $("#AnyElementId").hide(); How can I give the div id since all div has different ids
2

PHP:

<?php
$i = 0;
foreach ($query as $value) { ?>
<button id="show_<?php echo $i;?>" onclick=show(this.id) class="btn btn-success" type="button">
<div id="blah_<?php echo $i;?>">Joel</div>
<?php 
$i++ 
} ?>

javascript:

function show(id)
{
var arr = id.split('_');
$("#blah_"+arr[1]).hide("slow");
}

2 Comments

How to hide inside that javascript function?
Nice Work ... Perfectly for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.