3

I am using Bootstrap in my project and i need small wrapper. I get it from twitters source and it looks like:

section#my-content {
    background-color: #FFFFFF;
    border: 1px solid #DDDDDD;
    border-radius: 4px 4px 4px 4px;
    margin: 15px 0;
    padding: 39px 19px 14px;
    position: relative;
}
section#my-content:after {
    background-color: #F5F5F5;
    border: 1px solid #DDDDDD;
    border-radius: 4px 0 4px 0;
    color: #9DA0A4;
    content: "Example";
    font-size: 12px;
    font-weight: bold;
    left: -1px;
    padding: 3px 7px;
    position: absolute;
    top: -1px;
}

So is it possible with JS (jQuery) or or somehow different change content property dynamically ?

10
  • 1
    Have you tried using the jquery .css method? Commented Mar 25, 2013 at 21:18
  • @awbergs yes, no luck Commented Mar 25, 2013 at 21:19
  • 1
    Have you tried anything at all, as this should be fairly trivial to figure out with a Google search? At least you'll learn that jQuery can't access pseudo elements ! Commented Mar 25, 2013 at 21:19
  • 1
    Have you tried googling how to change inner content of elements with jquery / javascript? Commented Mar 25, 2013 at 21:19
  • 1
    Why are you using content? Would it possibly be a better idea to actually make what you're using a real html element? Is that possible? Commented Mar 25, 2013 at 21:22

5 Answers 5

15

You cannot modify pseudo-elements with jQuery (unless you want to add <style> tags). You can, however, change your CSS to make this easier:

content: attr(data-text);

The text will be contained within an attribute on the element:

<div data-text="This is the default text">Test</div>

Now, you can change the attribute with jQuery and the text will change:

$('h1').attr('data-text', 'This is some other text');

Demo: http://jsfiddle.net/8qNjv/

Sign up to request clarification or add additional context in comments.

Comments

0

if you meant, can you modify the css rules of a given css class using javascript/jQuery, then you cannot do so.

however, you can use jQuery .css to add new CSS rules dynamically.

read more about it here

Comments

0

You can use a lot:

// style editing
.attr()
.css()

// text editing
.html()
.append()

But maybe its better to add what you realy want, post also the html next time. And what you want as result.

Comments

0
**Angular template: Define myVar variable in ts file**

CSS

    .input-container{
     position: relative;
     }

   .input-container:after {
     position: absolute;
    right: 0px;
     top: 10px;
    content: attr(data);
   }

   
   <div class='input-container' [attr.data]="myVar">
    <input type='text'>
    <div>

 

Comments

0

Here is demo for modifying the css content parameter.

div:before {
  content: attr(data-cnt);
}
<div id="div1" data-cnt="1"> </div>
<input type="number" value=1 onchange="div1.setAttribute('data-cnt', this.value)">

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.