6

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.

5
  • 3
    concat with "+": serviceName = '<?=$_GET[' + services +'] . ".php";?>'; Commented Apr 29, 2014 at 9:43
  • 2
    This won't work, you're trying to return data from PHP to JavaScript. You'll need to make a call to your server (PHP) and return the value you need, then you can use this in JavaScript Commented Apr 29, 2014 at 9:45
  • 1
    Data from PHP to JavaScript should work fine Commented Apr 29, 2014 at 9:46
  • that code should work. try inserting alert($(serviceID).length), to see if you did get the element, if it alert a number greater than 0. Commented Apr 29, 2014 at 9:50
  • the code is correct... I have pasted your code on php file... look at this.. iwebtool.com/… Commented Apr 29, 2014 at 10:03

3 Answers 3

12

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');
}
Sign up to request clarification or add additional context in comments.

Comments

3

i hope this one solve the issue:

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

            console.log(serviceID);

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

                }
});
</script>

also check the console in your browser (firebug or chrome developer tools) to see the output ,fit your criteria

Comments

0

Try as but not sure, My change is append "#" Id in jquery

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

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

                }
});
</script>

1 Comment

For anyone else trying to see what the change was, the "#" has moved from the variable to the selector

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.