Ashwin
Ashwin

Reputation: 1230

How to find an element within an element using the id of the element to be fetched and passed as parameters?

I've been here and accordingly found out that this piece of code here

var node_exists=$(treeselector).find("li[id^='someid']");

where treeselector is the selector to the element within which the elements to be searched for are contained.This works perfectly fine.

However when in the id^='someid' part I try to change 'someid' to some variable which contains the id then it stops working.

var someid='someid'
var node_exists=$(treeselector).find("li[id^=someid]");

I also tried concatenating single quotes when the parameter is recieved in the function where this is fired. I think this might have to do with the double quotes surrounding the li[id^='someid'] part. Any idea as to how to make this work?

Cheers !!

Upvotes: 0

Views: 61

Answers (1)

Kristian
Kristian

Reputation: 21810

Concatenate:

var someVar = 'id1';
var node_exists=$(treeselector).find("li[id^="+someVar+"]");

Upvotes: 1

Related Questions