1

Ok,this is bothering me, i beleive it is simple but I am having trouble.

I have a css definition:

.isMobile{content:'No'}

And I have HTML:

<ins class="isMobile"></ins>

My question is how I read the content value into javascript using jQuery. I have tried, google and tried again and the closest I can get is

$("ins.cdo").css( "content" );

which returns "" not undefined. The desired result is either Yes or No.

Any help would be most appreciated.

3
  • 1
    Your JQuery is not targeting your HTML maybe?, otherwise it should work. Try removing the '.cdo' part Commented Jul 30, 2015 at 17:16
  • .isMobile{content:No} tried this? Commented Jul 30, 2015 at 17:17
  • 1
    Shouldn’t $('.isMobile').css('content') be enough? Commented Jul 30, 2015 at 17:19

4 Answers 4

2

The CSS content property is used with the :before and :after pseudo elements and shouldn't be used in the way you are intending.

Instead, this sounds like a good use case for data attributes. Here is how you would achieve your intended results with them:

First add the data-content attribute to your ins element, assigning it the value "No".

<ins class="isMobile" data-content="No"></ins>

Then with jQuery, you can get the value of the data attribute like this

$(".isMobile").data("content");

You can also change the value of the attribute like this

$(".isMovile").data("content", "Yes");
Sign up to request clarification or add additional context in comments.

Comments

0

This can fetch the value of the CSS property.

$("ins.isMobile").css( "content" );

Comments

0

The reason why you are unable to get the content property is because your selector is wrong try this

$("ins.isMobile").css("content");

Comments

0

I totally agree with @dhouty and the given answer, what you want is a bit weird and... it's an old one, but if you really want to do it, there are several ways to do it

  • either you parse the styleSheets inside your document
  • either you use window.getComputedStyle and its getPropertyValue

in both ways, YAGNI JQuery

the problem with the first option: you won't get the cascaded value, so let's use the second option which is more accurate.
in your case then, if you type:

window.getComputedStyle(
          document.querySelector("ins.isMobile")
     ).getPropertyValue("content")

you'll get the value "No"

in this command, document.querySelector("ins.isMobile") has its JQuery value as $("ins.isMobile")[0], getting the first element in your document that matches with your query

if, for some reason, you need a pseudo-selector, that's what we usually do when setting content, e.g. your css will look like this

.isMobile:before{content:"no";}

then window.getComputedStyle accepts a second argument which is your pseudo-element=>

window.getComputedStyle(
          document.querySelector("ins.isMobile"),
          ":before"
     ).getPropertyValue("content")

you'll get the value "no"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.