0

Hi im doing a PHP loop where I set a class, this loop make a list of items, and each one of them will have a "share" button that will open a window, (div display:none) the javascript open this div, and it works perfect..

My problem is that in this moment I have inserted the <script> code inside the loop so each item has its own <script></script> code...

Is there any way to do just one and take that code out of the php loop, so I can improve the code.

  <script>
  $('a.sharebtnc').click(
            function(event) {
                event.stopPropagation();
                $('div.redescompartir').fadeToggle(300);

            }
        );
  </script>

In the Loop well I just add to the class the id like this (id :32 )

  <script>
  $('a.sharebtnc32').click(
            function(event) {
                event.stopPropagation();
                $('div.redescompartir32').fadeToggle(300);

            }
        );
  </script>

How can I make this script works for all elements no matter the id number, I have no idea, but I know this is the correct way of doing this , right?

The HTML is very simple, its the same for each item... I mean each item has its own div with for <a href="" > </a> elements

<a href="#" class="sharebtnc(id)"> 
<div class="redescompartir(id)">
<a href="" >Twitter </a>
<a href="" >Facebook </a>
<a href="" >Google </a>
<a href="" >Foursquare </a>
</div>
2
  • What's the DOM look like? Is the input a child, parent, or sibling of the div? The right way to do this is to use this to refer to the clicked element, then traverse to the target you want to toggle. Commented Sep 28, 2013 at 0:04
  • the div is a normal div i will paste my code, but its a normal div with four a href elements. Commented Sep 28, 2013 at 0:20

2 Answers 2

2

Add a new class and attribute to the input element like

instead of

<a class="sharebtnc32" />

use

<a class="sharebtnc sharebtnc32" data-id="32"/>

then

jQuery(function ($) {
    $('.sharebtnctl').click(function (event) {
        event.stopPropagation();
        $('.redesopenclose' + $(this).data('id')).fadeToggle(300);
    });
})
Sign up to request clarification or add additional context in comments.

9 Comments

Sorry maybe i didnt make the right question , the class attribute its done in php loop so i get like 80 classes for example, with the class sharebtnid... (example sharebtn32 sharebtn33 etc) i need a universal code for all of them to save lines in the javascript
@MonchoChavez still you can have multiple classes for a element
i didnt see the last part, will acept in a min
@MonchoChavez see jsfiddle.net/arunpjohny/xX7A6/1 the class name redescompartir113 should be redesopenclose113
@MonchoChavez your script should be within a dom ready handler... see the update
|
1

My preferred method: PHP to generate link between the two elements, then a small snippet of JS to grab which particular one was clicked.

PHP:

$num = 100;

for ($x = 0; $x < $numItems; $x++) {
    echo "
    <a href='#' class='sharebtn' target='$x'>Share</a>

    <div class='redescompartir' id='$x'>
    <a href=''>Twitter</a>
    <a href=''>Facebook</a>
    <a href=''>Google</a>
    <a href=''>Foursquare</a>
    </div>
    ";

}

HTML output:

<a href="#" class="sharebtn" target='32'>Share</a>

<div class="redescompartir" id='32'>
<a href="" >Twitter </a>
<a href="" >Facebook </a>
<a href="" >Google </a>
<a href="" >Foursquare </a>
</div>

JS:

$('a.sharebtn').on('click', function(e) {
    e.stopPropagation();
    var target = $(e.currentTarget).attr('target');
    $('#'+target).fadeToggle(300);
});

This way you only need a single <script></script> tag, which your user's broswers will thank you for. :)

1 Comment

Thanks i will test i already have the PHP working fine :D, i know php my problem its with java thanks let me try

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.