Retrieve a pseudo element's content property value using JavaScript

aledroner

I have the following jQuery code:

$.each($(".coin"), function() {
    var content = "/*:before content*/";
    $("input", this).val(content);
});

I'd like to change the value of each input element using jQuery based on its pseudo element's content property value (.coin:before).

Here a example: http://jsfiddle.net/aledroner/s2mgd1mo/2/

Josh Crozier

According to MDN, the second parameter to the .getComputedStyle() method is the pseudo element:

var style = window.getComputedStyle(element[, pseudoElt]);

pseudoElt (Optional) - A string specifying the pseudo-element to match. Must be omitted (or null) for regular elements.

Therefore you could use the following in order to get the pseudo element's content value:

window.getComputedStyle(this, ':before').content;

Updated Example

$('.coin').each(function() {
  var content = window.getComputedStyle(this, ':before').content;
  $("input", this).val(content);
});

If you want to get the entity code based on the character, you can also use the following:

function getEntityFromCharacter(character) {
  var hexCode = character.replace(/['"]/g, '').charCodeAt(0).toString(16).toUpperCase();
  while (hexCode.length < 4) {
    hexCode = '0' + hexCode;
  }

  return '\\' + hexCode + ';';
}
$('.coin').each(function() {
  var content = window.getComputedStyle(this, ':before').content;
  $('input', this).val(getEntityFromCharacter(content));
});
.dollar:before {
  content: '\0024'
}
.yen:before {
  content: '\00A5'
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="coin dollar">
  <input type="text" />
</div>
<div class="coin yen">
  <input type="text" />
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

Change content property of pseudo element within CSS animation

From Dev

Pseudo element content with JQuery

From Dev

automate the content value of pseudo element depending which number the child is

From Dev

automate the content value of pseudo element depending which number the child is

From Dev

Dynamic styling for pseudo element's content in Vue2.x

From Dev

Vertically/horizontally centering a pseudo element's generated content

From Dev

Dynamic styling for pseudo element's content in Vue2.x

From Dev

Using the :after CSS pseudo-element without inserting content

From Dev

How to apply a CSS property to an element using it's CSS path in Javascript?

From Dev

How can I retrieve the text of a clicked element using jQuery's .text property?

From Dev

Retrieve enum value using unique property

From Dev

how to retrieve the value of a property using the value of another property in RDDs

From Dev

how to retrieve the value of a property using the value of another property in RDDs

From Dev

How to set the content property of an `after` pseudo-element to an attribute of a sibling form element

From Dev

Pseudo element using ::before overflow's parent element height

From Dev

How to retrieve content (element by element) of a external website using AngularJS

From Dev

Retrieve XML nodes by Element Value with Javascript or JQuery

From Dev

disable touch on the pseudo content of an element

From Dev

Updating pseudo-element content property when HTMLElement.dataset updates

From Dev

Unicode in data-content for pseudo element content

From Dev

Not display ::ms-clear pseudo element using JavaScript

From Dev

Setting a HTML Date element's value with a PHP variable using Javascript

From Dev

Use content of an element in attr() of pseudo-element

From Dev

Repeat content value of pseudo selector

From Dev

Get content of a <title> element in html using JavaScript

From Dev

Get content of a <title> element in html using JavaScript

From Dev

before pseudo-element covering inner content

Related Related

  1. 1

    CSS Transition on the 'content' property of :before pseudo element

  2. 2

    CSS Transition on the 'content' property of :before pseudo element

  3. 3

    Change content property of pseudo element within CSS animation

  4. 4

    Pseudo element content with JQuery

  5. 5

    automate the content value of pseudo element depending which number the child is

  6. 6

    automate the content value of pseudo element depending which number the child is

  7. 7

    Dynamic styling for pseudo element's content in Vue2.x

  8. 8

    Vertically/horizontally centering a pseudo element's generated content

  9. 9

    Dynamic styling for pseudo element's content in Vue2.x

  10. 10

    Using the :after CSS pseudo-element without inserting content

  11. 11

    How to apply a CSS property to an element using it's CSS path in Javascript?

  12. 12

    How can I retrieve the text of a clicked element using jQuery's .text property?

  13. 13

    Retrieve enum value using unique property

  14. 14

    how to retrieve the value of a property using the value of another property in RDDs

  15. 15

    how to retrieve the value of a property using the value of another property in RDDs

  16. 16

    How to set the content property of an `after` pseudo-element to an attribute of a sibling form element

  17. 17

    Pseudo element using ::before overflow's parent element height

  18. 18

    How to retrieve content (element by element) of a external website using AngularJS

  19. 19

    Retrieve XML nodes by Element Value with Javascript or JQuery

  20. 20

    disable touch on the pseudo content of an element

  21. 21

    Updating pseudo-element content property when HTMLElement.dataset updates

  22. 22

    Unicode in data-content for pseudo element content

  23. 23

    Not display ::ms-clear pseudo element using JavaScript

  24. 24

    Setting a HTML Date element's value with a PHP variable using Javascript

  25. 25

    Use content of an element in attr() of pseudo-element

  26. 26

    Repeat content value of pseudo selector

  27. 27

    Get content of a <title> element in html using JavaScript

  28. 28

    Get content of a <title> element in html using JavaScript

  29. 29

    before pseudo-element covering inner content

HotTag

Archive