0

Select data-ctrtid custom attribute to set style

HTML snip

<div class="expandListHeaderRow" data-ctryid="28fa1d89-eabe-44b5-b761-897b7835d7ab">South Africa</div>

This is dynamically created code and I capture the id correctly like this.

$('.expandListDetail').on('click', '.expandListHeader', function(){
   var argument = $(this).closest('.expandListHeader').find('div').data('ctryid');
   .
   .
}

But after I refresh the list, I want to expand the list to the place where it was before. (this is a dynamic multi expandable/Collapsible list)

data('ctryid').css('display': 'block') or somthing like this.

But how do you search for the specific ID of the custom attribute to target the element for CSS change?

DaniP
  • 37,813
  • 8
  • 65
  • 74
morne
  • 4,035
  • 9
  • 50
  • 96
  • http://stackoverflow.com/a/5324433/3691686 – marcelo2605 Oct 31 '14 at 14:55
  • @marcelo2605 No not exactly. this is setting the style for a before hand ( i think). I have atleast 20 `data-ctryId`s so will need to target only the specific one – morne Oct 31 '14 at 15:00

2 Answers2

0

If you want to search for an id by string variable value, convert it to a jQuery ID selector (i.e. prefix with #):

$('#' + data('ctryid'))

so your example becomes:

$('#' + data('ctryid')).css('display': 'block')

This of course assumes you have elements like this somewhere in the page:

<div id="28fa1d89-eabe-44b5-b761-897b7835d7ab">
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
0

If I understand correctly, you have the id, and you need to find the element. If so, this will work:

$('[data-ctryid="' + capturedId + '"]')

See http://api.jquery.com/attribute-equals-selector/

Talmid
  • 1,273
  • 14
  • 19
  • I though that at first, but *they already have that element* as they found it and retrieved the `data('ctryid')` from it. – iCollect.it Ltd Oct 31 '14 at 15:40
  • @TrueBlueAussie Yes, perhaps you are right. It just seemed like his jQuery knowledge extended beyond the basic concept of selecting an element by id. Maybe the OP will clarify. – Talmid Oct 31 '14 at 15:45
  • I hope they clarify. The question is ambiguous at best. While true, your example is pointless though, as they *already have* the element that has that attribute so a search returns the same element they already have. – iCollect.it Ltd Oct 31 '14 at 15:46