0

How to add var javascript value into id name ?

I have $i = 5; and var i = 2;

How to concatenate php var $i with javascript var i ?

I tried like this but not work.

$('#test<?PHP echo $i; ?>'+i).live('click', function() { 
do something
});

and

$('#test<?PHP echo $i; ?>'+i+'').live('click', function() { 
do something
});

How can i do that ?

mongmong seesee
  • 987
  • 1
  • 13
  • 24

1 Answers1

0
var phpi = '<?PHP echo $i; ?>';
$('#test' + phpi + i).live('click', function() { 
    //do something
});

or if you want PHP $i + JS i for exmaple (3 + 2 = 5)

var phpi = <?PHP echo $i; ?>; // no quotes here as we need number
$('#test' + (phpi + i)).live('click', function() { 
    //do something
});

PHP echo makes some space so that might hamper the selector use it like this

joyBlanks
  • 6,419
  • 1
  • 22
  • 47