4

I want to change a jquery command via PHP.

So for example:

$envelope<?php echo'1'?>.toggleClass( 'card-out' );

is this valid to use? I tried it and i think it didn't work, but i don't know if the mistake is in that line or in the rest of the code.

this is the main part of my code:

<div class="library-card envelope<?php echo'2'?>">
    <div class="front">
        <p class="stamp">Jacob Haase<br> 18.05.2000</p>
    </div>
    <div class="card">sdsdsd</div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        var $envelope = $( '.library-card.envelope' ),
            $card = $envelope.find( '.card' ),
            $front = $envelope.find( '.front' );

        $front.on( 'click', function( event ) {
            $envelope<?php echo'2'?>.toggleClass( 'card-out' );
        });

        $card.on( 'click', function( event ) {
            $envelope<?php echo'2'?>.toggleClass( 'card-out' );
        });
    </script>

</div>

thank you!

9
  • 1
    it won't work if it is inside the js file. there is a chance to work if it is in php file Commented Jul 27, 2015 at 11:17
  • 1
    Sure, it's valid (if <?php echo '1' ?> is meaningful there. PHP and in-browser JS won't interfere if you know what you're doing. The question is: Should you do this? Commented Jul 27, 2015 at 11:18
  • I agree with Tushar, it might work, depending on the rest of your code. But I wouldn't suggest it. Better trigger it on a class or id. Commented Jul 27, 2015 at 11:18
  • is there a way around that? Commented Jul 27, 2015 at 11:18
  • 1
    @EliasVanOotegem I don't think so, It's more readable when you prefix a jQuery object with $. So, you know it's a jQuery object, and you can call jQuery methods on it Commented Jul 27, 2015 at 11:21

2 Answers 2

3

It's perfectly valid if you are using jQuery in .php file.

In this line:

$envelope<?php echo'1'?>.toggleClass( 'card-out' );

This will be $envelope1.toggleClass('card-out');, but I'm not sure if you have $envelope1 jQuery variable. But may be, you are trying to do like this:

$('.envelope1').toggleClass('card-out');

If so, use:

$(".envelope"+<?php echo'1';?>).toggleClass( 'card-out' );
Sign up to request clarification or add additional context in comments.

6 Comments

someone are waiting for down vote without a reason. :(
@BhojendraNepal I removed the down vote after the edit. The code before had would not run.
You are encouraging the OP to mix PHP with Javascript which is bad practice, I'm sure there'll be other/better way
@BhojendraNepal I think he added ';'
@Tushar, Yes, sometimes we need to integrate php variable into jquery, and this is needed and may not be bad idea because there may not be any idea beyond this. If you have please do write it in your answer.
|
1

If you are trying to bound some PHP code with the click event then this is impossible in the way you are trying and PHP code will be executed as soon as page load without waiting for a click event.

If you are trying to generate final javascript or jquery code using PHP then this is okay.

for more info Is it okay to use PHP inside a jQuery script?

for your problem use like this

$(".envelope"+<?php echo'1';?>).toggleClass( 'card-out' );

for Learning http://wptricks.co.uk/include-php-variables-into-jquery/

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.