0

I have a div in my form

<div id="s2id_s52dcecf43a846_membership">
<div>

I want to find the div by the name "membership" because the other elements of the id are randomly generated.

How to get the div by just getting the partial value of the div id ?

I want to get it via jQuery/Javascript.

Thanks,

FaisalNasir

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
faisal nasir
  • 165
  • 4
  • 14

2 Answers2

2

If the id starts with "membership" and then comes the random part you can use:

$('[id^="membership"])

You can also use "contains" selector, like

$('[id*="membership"])

If you want to search by the name, you can use

$('[name=membership]');

Keep in mind this might give you more than one element.

You can also check more selectors here

Sergio
  • 28,539
  • 11
  • 85
  • 132
  • Is there a way to get this via raw javascript, such as document.getElementById combined with a regular expression or something? – code-sushi Jun 12 '18 at 17:15
  • @code-sushi such complex selectors are not available in CSS. Its possible but you need to iterate many elements and will end up with many lines of code. – Sergio Jun 12 '18 at 18:42
  • Just for the record it can be done using CSS; my question was about raw javascript. I found the answer here: https://stackoverflow.com/questions/15874630/get-element-by-part-of-name-or-id#15874766 For CSS, one would do as follows (assuming you are looking for anything with "member" in it) div[id*='member'] { /* your css goes here */ } Thanks for pinging me to follow up, though! – code-sushi Jun 12 '18 at 19:04
1

just getting the partial value of the div id

Just try the attribute contains selector,

$('[id*="membership"])

Or the better way would be add a common class to those elements

Please read here to know more about Jquery selectors

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130