tawheed
tawheed

Reputation: 5821

Jquery string concatenation

I might be overlooking the obvious but I am trying to do a basic string concatenation and it does not seem to play to well

I have the following variables

var plug = "myPlug";
var pdiv = "<p></p>";
var tab_id = "#someID";

$(tab_id + "p:contains('" + plug + "')").parent().next().append(pdiv);

For some reason tab_id does not get substituted. Any help would be appreciated.

Upvotes: 1

Views: 4288

Answers (3)

Sergio
Sergio

Reputation: 28845

You need to leave a space between #someID and p:contains. Concatenation does not add spaces by itself.

You need to use:

$(tab_id + " p:contains('" + plug + "')").parent().next().append(pdiv);
            ^
          give extra space here

Upvotes: 2

David
David

Reputation: 219037

It looks like you're missing a space. Currently your selector will evaluate to:

$("#someIDp:contains('myPlug')")

which... doesn't look right. Try adding a space after the ID and before the child p element:

$(tab_id + " p:contains('" + plug + "')")

Upvotes: 1

epascarello
epascarello

Reputation: 207537

because you are looking for

("#someIDp:contains....")
        ^^
      No space

Upvotes: 2

Related Questions