0

I am trying to make dynamic button thing where the selected section will enlarge when button is pressed.

.php

<?php
    while($row = $results->fetch())
    {
    ?>
        <section class="rss-container">
            <p class="rss-desc">
            <?php                                                                                    
                echo '<div class="dmitri" id="d'.$row['id_ilmoitus'].'">'.$row['popup'].'</div>';
                echo '<div class="vladmir" id="v'.$row['id_ilmoitus'].'">'.$row["teksti"].'</div>';                                                   
            ?>
            </p>    
            <br />
            <span class="rss-badge more no-underline" id="button_<?php echo $row['id_ilmoitus'] ?>">Laajenna</span>
        </section>
        <br />
    <?php
    }
?>

.jquery

$('.vladmir').hide();
    $('.rss-badge').click(function(){
    $('d'+ ? ).toggle();
    $('v'+ ? ).toggle();
});

I have no idea how to $row['id_ilmoitus] there. I tried to make it as variable but didnt work.

Sorry for being unclear.

5
  • Access the elements by the class not the id (dmitri and vladmir). Commented May 21, 2014 at 11:03
  • no id should be used because the button is supposed to enlarge only the appropriate section not all at once. Commented May 21, 2014 at 11:06
  • Yes only one at a time. Commented May 21, 2014 at 11:08
  • Is your jQuery in a separate file? Commented May 21, 2014 at 11:14
  • No, its right under loop. Commented May 21, 2014 at 11:15

4 Answers 4

2

I would suggest you to add idilmoitus as custom data-* attribute which you can access using .data()

HTML

<span data-idilmoitus="<?php echo $row['id_ilmoitus'] ?>" class="rss-badge more no-underline" id="button_<?php echo $row['id_ilmoitus'] ?>">Laajenna</span>

JQuery

$('.vladmir').hide();
$('.rss-badge').click(function(){
    var idilmoitus = $(this).data('idilmoitus');
    $('#d'+ idilmoitus ).toggle();
    $('#v'+ idilmoitus ).toggle();
});

Another way is to traverse DOM

$('.rss-badge').click(function(){
    var section = $(this).closest('.rss-container');
    $(section).find('.dmitri').toggle();
    $(section).find('.vladmir').toggle();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this worked. I actually made this before but forgot to add #. Thanks!
0

you can use onclick and send the parameter to the function like this:

<span onclick="buttonClicked('<?php echo $row['id_ilmoitus'] ?>')" class="rss-badge more no-underline" id="button_<?php echo $row['id_ilmoitus'] ?>">Laajenna</span>

and javascript:

function buttonClicked(objId)
{
    $('d'+ objId ).toggle();
    $('v'+ objId ).toggle();
}

Comments

0

Try this:

$('.rss-badge').click(function(){
    var attrib = $(this).attr( "id" );
    attrib = attrib.split('_')[1];

    $('#d' + attrib ).toggle();
    $('#v' + attrib ).toggle();
});

Comments

0

This is working example, you will not need the ID as well, with CSS class only you can do all the operation.

@Fiddle : http://jsfiddle.net/pragneshkaria/AJ5xJ/

JQUERY

   $('.rss-badge').click(function (){
    //alert('hi');
    $('.rss-container').css( "background-color", "" );
    $('.dmitri').css( "background-color", "" );
    $('.vladmir').css( "background-color", "" );

    $(this).parent().css( "background-color", "green" );

    $(this).parent().children().next(".dmitri").css( "background-color", "yellow" );

     $(this).parent().children().next(".vladmir").css( "background-color", "blue" );
});

HTML

<section class="rss-container">
<p class="rss-desc">                                                 
    <div class="dmitri" >D 1</div>
    <div class="vladmir" id="v1"> V 1</div>                                                  
</p>
<br />
<span class="rss-badge more no-underline" id="button_1">Laajenna 1</span>
</section>
<br />
<section class="rss-container">
<p class="rss-desc">                                                 
    <div class="dmitri" >D 2</div>
    <div class="vladmir" id="v1">V 2</div>                                                  
</p>
<br />
<span class="rss-badge more no-underline" id="button_1">Laajenna 2</span>
</section>
<br />
<br />
<section class="rss-container">
<p class="rss-desc">                                                 
    <div class="dmitri" >D 3</div>
    <div class="vladmir" id="v1">V 3</div>                                                  
</p>
<br />
<span class="rss-badge more no-underline" id="button_1">Laajenna 3</span>
</section>
<br />

CSS

.rss-container{
    border:1px solid red;
}
.rss-badge{
    border:1px solid blue;
    cursor:pointer;
}
.dmitri{
    border:1px solid #ccc;
}
.vladmir{
    border:1px solid #ccc;
}

Hope this will help you. You can modify the code as per your need.

I have just added three static divs with different id.

Comments

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.