Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  196] [ 2]  / answers: 1 / hits: 84835  / 10 Years ago, tue, april 29, 2014, 12:00:00

Just like the title above: how to concatenate two strings in JQuery?



This is my code so far:



$(document).ready(function(){           
serviceName = '<?=$_GET['services'] . .php;?>';
serviceID='<?= # .$_GET['services'];?>';
serviceClass = '<?=$_GET['services'];?>';

if (serviceName != ) {
alert(serviceID);
$('.main_content').load(serviceName);
$(serviceID).addClass(it);
}
});


As you can see in my above code, in variable name serviceID, I am concatenating hashtag and my GET value and I try to put it on ALERT and the result is correct, but when I assign it to .addClass it’s not working.



Any alternatives and solution is much appreciated.


More From » jquery

 Answers
6

I guess you meant that the PHP code should be evaluated before arriving to the client, therefore your code syntax is correct, but check out the following looks cleaner and also not polluting the global JavaScript scope (that's what the var ... is for):



var serviceClass = '<?={$_GET[services]};?>';
var serviceName = serviceClass+'.php';
var serviceId = '#'+serviceClass;


but, since your code syntax is correct, you should verify that you actually have an element with serviceId as the id when your are executing it



if (($(serviceId)||[]).length) {
alert('your element with the id:'+serviceClass+' exists');
}
else {
alert('your element with the id:'+serviceClass+' doesn't exists');
}

[#71257] Sunday, April 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikokorbinm

Total Points: 744
Total Questions: 126
Total Answers: 104

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;