0

With my code I can change my text by click:

$('.SeeMore').click(function(){
		var $this = $(this);
		$this.toggleClass('SeeMore');
		if($this.hasClass('SeeMore')){
			$this.text('+ more');			
		} else {
			$this.text('- less');
		}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="SeeMore" style="cursor:pointer">+ more</li>

This is working well so far. But because I have different language versions of my page I want to exchange the words with php:

   <script> 
       $('.SeeMore').click(function(){

        var more = <?php echo $lang['MORE']; ?>
        var less = <?php echo $lang['LESS']; ?>
        var $this = $(this);
        $this.toggleClass('SeeMore');
        if($this.hasClass('SeeMore')){
            $this.text(more);           
        } else {
            $this.text(less);
        }
    });</script>   

Unfortunately now my code is not working anymore and I do not know why.

4
  • 3
    You need to use ", Ex: var more = "<?php echo $lang['MORE']; ?>" Commented Sep 21, 2016 at 8:39
  • try echoing the whole <script> Commented Sep 21, 2016 at 8:39
  • 1
    @deviantxdes, The OP needs to use PHP inside jQuery... Commented Sep 21, 2016 at 8:39
  • 1
    @FrayneKonok Thank you, this is working very well! Commented Sep 21, 2016 at 8:42

6 Answers 6

2

There are two things that is missed by you-

 1. Semi-colon at the end of the statement.
 2. Double / single quotation around the PHP statement inside the jQuery.

You need to use ", Ex: var more = "<?php echo $lang['MORE']; ?>";

Sign up to request clarification or add additional context in comments.

Comments

1

You forgot the semicolons and quotes:

var more = "<?php echo $lang['MORE']; ?>";
var less = "<?php echo $lang['LESS']; ?>";

writing code-generating-code is hard and can be confusing. Better use a decent template system (e.g. Smarty).

Comments

1

You are forgetting the quotes:

var more = "<?php echo $lang['MORE']; ?>";
var less = "<?php echo $lang['LESS']; ?>";

Comments

1

Use data attributes

html:

<li class="SeeMore" data-less="<?php echo $lang['LESS']; ?>" data-more="<?php echo $lang['MORE']; ?>" style="cursor:pointer">+ more</li>

js:

   $('.SeeMore').click(function(){

        var more = $(this).attr('data-more');
        var less = $(this).attr('data-less');
        var $this = $(this);
        $this.toggleClass('SeeMore');
        if($this.hasClass('SeeMore')){
            $this.text(more);           
        } else {
            $this.text(less);
        }
    });

PS: don't forget to wrap you click event in a document ready statement

Comments

1

You can use your php variables inside jquery with the following code:

token = "<?php echo $_SESSION['token']; ?>"

Comments

0

use this.

var more = <?php echo json_encode($mymore); ?>;

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.