Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

My question is quite simple but I cannot find the truth
It is possible to use regex in Javascript onclick event (HTML)?
for example is it possible to show div with different id but a same tag using regex :

div s:

<div class='zone' id='{{site.City}}'>
<div class='zone' id='{{site.City}}{{immo.CodeImmo}}'>
<div class='zone' id='{{site.City}}{{sth else}}'>

javascript regex :

onclick='$("#/^{{site.City}}./").show();'

I am worried about the regex syntax in the Javascript onclick event.
Is it possible? and how is it possible to do it?
This question is for my personal knowledge. Thanks

EDIT :
ok I know now I can use:

onclick=`$('div[id*="{{site.City}}"]').show();`

but how can I do if {{site.City}} is a chain variable?
What is the syntax for a loop in onclick even with a django dictionnary variable?
It's not the topic. It is still for my knowledge. thx

share|improve this question
    
Why not just attach the event handlers using the class name? – NewToJS Jun 28 at 7:32
    
Take a jQuery selector, ie $('.zone'). – Jan Jun 28 at 7:33
    
You don't seem to need a regex, you can use the attribute starts with selector. – nnnnnn Jun 28 at 7:34
    
well I dont use selector with class because for the same class I wanted different event That's why I use id. I did what I wanted using class but just wanted to know how I can possibly works with id and regex – Stages Jun 28 at 7:45
    
@Stages: Just use another class then. Or data – Jan Jun 28 at 7:47
up vote 1 down vote accepted

To my knowledge, it is impossible to use RegEx in jQuery selectors. You can, however, use css selectors for these IDs. This will match all divs with an id that contains "{{site.City}}".

div[id*="{{site.City}}"]

That would make your onClick attribute look like this (note the different quotation marks):

onclick=`$('div[id*="{{site.City}}"]').show();`

If you would like the ids to start with {{site.City}}, use:

div[id^="{{site.City}}"]

More details on selectors can be found on the MDN page.

share|improve this answer
    
yes its a good idea but there are one matter here : you should know that site.City is for a list/dictionnary with several cities so how can I use this kind of selectors to select only (for example) 2 City on 5? and btw the City Immo and sth else. I know this is kind of wierd. – Stages Jun 28 at 7:59

Try using a class selector:

<div class='zone site-city' id='{{site.City}}'>
<div class='zone site-city' id='{{site.City}}{{immo.CodeImmo}}'>
<div class='zone site-city' id='{{site.City}}{{sth else}}'>

<a onclick='$(".site-city").show();'>Some anchor</a>

EDIT: AFAIK it's not possible to use regular expressions as selectors with jQuery. The closest approach is to use "starts-with" ([attr^=start]) and "ends-with" ([attr$=end]) selectors, which in your case would be:

onclick='$("[id^='{{site.City}}']").show();'

EDIT 2: If you really need to match elements based on complex patterns, then you might need to do it in two steps:

$("div.zone").filter(function () {
    return /your regex here/.test($(this).attr('id'));
}).show();
share|improve this answer
    
thx I know this works but it does not repond to my question. I am worried about how doing this using only id and maybe regex to do things work. – Stages Jun 28 at 7:47
    
I get it. Check my edit. – Danilo Valente Jun 28 at 7:56
    
ok thx it's the same idea of @Mark Langer. So how can I do know if site.City is a caracter chain variable? for example : NewYork, Chicago, LosAngeles, Denver... and I dont want to show only the things for Chicago ans NewYork? Do you thing it is possible using for in the onclick event? – Stages Jun 28 at 8:03
    
It is, but only if you prefix your id attributes like: id='city-{{site.City}}' then you can onclick='$("[id^='city-']").show();'. – Danilo Valente Jun 28 at 8:06
1  
However, I strongly recommend you use classes since they were invented to solve problems like yours. – Danilo Valente Jun 28 at 8:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.