2

i found a link to get the id's which are ending with a specific string.

  $("element[id$='txtTitle']")

How can we get the id's if ending strings are many. Like i have an array of strings and want all id's that are ending with anyone of these strings.

Thank's

Community
  • 1
  • 1
Mansoor Jafar
  • 1,390
  • 3
  • 14
  • 31

4 Answers4

3

You can combine selector with different endings with comma.

$("element[id$='txtTitle1'],  element[id$='txtTitle2'])")

When you have different ending and same start use start.

$("element[id^='txtTitle']")

When you have some text common but not sure if it is in start or end or middle use *

 $("element[id*='txtTitle']")
Adil
  • 143,427
  • 25
  • 201
  • 198
1

If you have an array of strings containing the ending of the id you can loop over them and add them to a collection. Try this:

var $elements = $(); // empty jQuery object
var ids = ["txtTitle", "txtFirstname", "txtLastName"];
$.each(ids, function(index, value) {
    $elements.add("element[id$='" + value + "']");
});

// $elements is now a collection you can use eg:
$elements.css("background-color", "#C00");
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
0

Build up the selector first:

var endings = ["a","b","c"];
var selector = "";

for(var i = 0, len = endings.length; i < len; i++) {
   selector += "element[id$='" + endings[i] + "']";

   if (i != (endings.length - 1)) selector += ",";
}

Then do the selection:

var el = $(selector);
Lloyd
  • 28,624
  • 4
  • 82
  • 95
0

So you have an array of endings

var arr = ["ending1","ending2"];

You can use the jquery .map() function to build up a multiple selector (separated by a comma)

var selector = $.map(arr,function(i,e){return "element[id$='" + e + "']";})
                .join(",");
// result: "element[id$='ending1'],element[id$='ending2']"

Then use the selector:

var $elements = $(selector);
Jamiec
  • 128,537
  • 12
  • 134
  • 188