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
  • 757
  • 1
  • 9
  • 23

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
  • 5,894
  • 1
  • 15
  • 42