Is there a way to use variable CSS selector which can selectively apply css to html element which has classes which is a variable?

Sallu

I have say 3 spans as below :

<span  class = "testVar1" onClick = "testFunction(Var1)">
<span  class = "testVar2" onClick = "testFunction(Var2)">
<span  class = "testVar3" onClick = "testFunction(Var3)">
testFunction(var){
    here I assign class "on" to the span which calls this function
}

If span with class testVar1 calls this then it becomes

<span  class = "testVar1 on" onClick = "testFunction(Var1)"></span>

My Css is as below

.test .on {
    some CSS
}

Is there a way in CSS where I can use a variable and apply css to those span which is clicked? Like

.test[Var1 or Var2 or Var3] .on {
    some CSS
}

I have achieved it by using multiple selectors manually like#

.testVar1 .on {
    some CSS
}
.testVar2 .on {
    some CSS
}

I have read the post Using regular expression in css? , it,s helpful but not answering my question.

In this post css is applied to all the element, but I want css to be applied only to the element which called the function. and so on.

Any help or pointers would be appreciated! Thanks!

connexo

You are making things too complicated. Just use the same CSS class on all of them, then add the click listener programmatically, not as an inline onlick listener:

document.querySelectorAll('span.test').forEach(
  span =>
  span.addEventListener('click', () => {
      console.log(`you clicked ${span.innerText}`) 
      span.classList.toggle('on')
  })
)
.test {
  background: red;
  color: white;
  display: inline-block;
  padding: 40px;
}

.test.on {
  background: green;
}
<span class="test">foo</span>
<span class="test">bar</span>
<span class="test">baz</span>

If you insist on inline event listeners (you really shouldn't, it's widely considered bad practice), for this simple example it's probably even easier:

function foobar(span) {
  console.log(`you clicked ${span.innerText}`)
  span.classList.toggle('on')
}
.test {
  background: red;
  color: white;
  display: inline-block;
  padding: 40px;
}

.test.on {
  background: green;
}
<span class="test" onclick="foobar(this)">foo</span>
<span class="test" onclick="foobar(this)">bar</span>
<span class="test" onclick="foobar(this)">baz</span>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Apply two CSS classes which contain `filter` to an HTML element

how to use CSS variable which has multiple property

How can I use CSS in tr which has class in html?

find and click element which has a changing CSS selector (python)

Which CSS selector is stronger?

Which are the limits of CSS :not() selector?

Can i add a comma (,) dynamically to a CSS pseudo element which its value is coming from CSS variable

How to apply css to div which has no class?

CSS selector for the next element of an element which fires onclick?

Which is faster in jquery: $("selector") or selector as an object variable?

Restrict CSS to apply in which DIV

Opacity on first and last element of slider which has ".active" classes using javascript or css

How can I use different variables to calculate a new variable, depending on which variable has missing values?

if we apply some properties in both html and css code which one will apply first and which will be finally applied

add tooltip to element which has css text-overflow:ellipses

CSS: Any way to horizontally center a header which has "position:fixed"?

Selecting Element which Has Exact Combinations of Classes

How to find element which has 2 classes

Does there exist a way by which i can use WMI classes in java

Is there a way to get which classes a ClassLoader has loaded?

CSS library or theme which does not need html classes

Can we use a variable which has endpoint URL directly in HTTP Request UIPath Activity?

Is there a way to detect which CSS grid column and row an element is in using javascript?

Is there the HTML which is being ignored by CSS?

How to create HTML element which takes value from variable

Is it possible to save an html element with child elements as JS variable which you can dynamically append to the DOM?

How to add CSS to a HTML div which has no class or id

A variable of which a part is variable

How to know which variable has a value and which one is null?