views:

543

answers:

4

Html element contains complex & unique id, composed from namespace as prefix and incremented index - as postfix.

I try do it with wildcard expression for id.

If to use simple way without namespace, that it works fine:

$j("[id*=service_selected_]").click(function(){
...

In order to keep uniqueness,i need provide namespace part within id,but it doesn't resolved this way:

var selId = "'" + '[id*=' + namespace + '_service_selected_]' + "'";

$j(selId).click(function(){
...


Below the .jsp part:

     <c:forEach var="package" items="${packages.page}" varStatus="status">
      <tr>
       <td>
        ${package.name}
       </td>
       <td id="<portlet:namespace/>_service_price_${status.index}">${package.price}</td>
       <td >
<input type="checkbox" name="service_id" value="${package.id}" id="<portlet:namespace/>_service_selected_${status.index}">
       </td>
      </tr>
     </c:forEach>
+2  A: 

How about

var selId = "[id*=" + namespace + "_service_selected_]";
$j(selId).click(function(){
...

I think you've got confused with too many quotation marks!

Phil
i tried already, it didn't help
sergionni
Your selector works, your just needed to prepend an underscore to the last string literal. That is; `+ "service_selected_]";` should be `+ "_service_selected_]";`.
brianpeiris
Thanks Brian, edited.
Phil
+1  A: 

You might search twice

$j("[id*=" + namespace + "]").filter("[id*=service_selected_]").click(function(){
Ghommey
namespace is a variable...mmm..seems this case i need use .matchsomething like that:$j(this).attr('id').match(namespace).filter("[id*=service_selected_]").click(function(){
sergionni
k I changed it to be a variable
Ghommey
+2  A: 

I would use the startsWith selector to get the namespace of elements, and then query against it to get your matches...

var namespace = "test";
var itemspace = "_service_selected_";

var ns = $("[id^=" + namespace + "]");
var nsItems = $("[id*=" + itemspace + "]", ns);
Josh Stodola
I'm afraid your code doesn't work as you expect. jQuery will attempt to select from the child elements of each element in the `ns` set, instead of from the elements in the set themselves. You could make it work by appending the `ns` set to a new `<div/>` but that only adds to the amount of processing compared to the simpler solutions.
brianpeiris
+3  A: 

Well, since we are all suggesting different versions of the same code, here's mine:

var namespace = 'test';
var id = '_service_selected_';
$(["[id^=", namespace, "_", id, "_]"].join(''));

I've also done some speed tests on all the suggestions posted so far. Mine happens to be the fastest on Firefox and Chrome and the second best on IE8. Josh Stodola's doesn't work at all.

Here's the test suite: http://jsbin.com/igate (Editable via http://jsbin.com/igate/edit)

Here are the results:

Firefox

select_Phil 37 ms 36 selected
select_Ghommey 120 ms 36 selected
select_Josh_Stodola 1077 ms 0 selected
select_brianpeiris 31 ms 36 selected

Chrome

select_Phil 17 ms 36 selected
select_Ghommey 108 ms 36 selected
select_Josh_Stodola 290 ms 0 selected
select_brianpeiris 15 ms 36 selected

IE8

select_Phil 69 ms 36 selected
select_Ghommey 300 ms 36 selected
select_Josh_Stodola 1632 ms 0 selected
select_brianpeiris 73 ms 36 selected
brianpeiris
Nice analysis! Wonder if I can shave some ms off my time!
Phil
Did a quick test, turns out the only difference between your selector and mine is that your's uses the `*=` ('contains') selector, whereas mine uses the `^=` ('begins with') selector which give it a slight speed up. My attempt to optimize with the `join()` method seems to be useless, except on IE8 where it's worse!
brianpeiris