2

I want to replace the content from a tag in CSS via jQuery.

In this case the content "My content" to "New Content".

I have this CSS code :

a.appari:focus:after{
  background: #333;
  background: rgba(0,0,0,.8);
  border-radius: 5px;
  bottom: 26px;
  color: #fff;
  content: "My content";
  left: 20%;
  padding: 5px 15px;
  position: absolute;
  z-index: 98;
  width: 400px;
}

I tried this jQuery code :

function prova() {
{
 ?>
<script>
(function($){
  $(document).ready(function(){
    $('.appari').click(function(){         
      $("<style>.appari:focus:after { content: 'New Content'; }</style>").appendTo( "head" )
  });
});

})(jQuery);
</script>

<?php

  }
}

Thanks for helping.

0

1 Answer 1

2

You're running into a CSS specificity issue. Change the selector in your jQuery to have enough or more specificity than the original rule. You can just add an a to .appari so it's a.appari (matches your original CSS specificity)

$("<style>a.appari:focus:after { content: 'New Content'; }</style>").appendTo("head")

$('.appari').click(function() {
  $("<style>a.appari:focus:after { content: 'New Content'; }</style>").appendTo("head");
});
a.appari:focus:after {
  background: #333;
  background: rgba(0, 0, 0, .8);
  border-radius: 5px;
  content: "My content";
  padding: 5px 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="appari">foo</a>

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

1 Comment

Thank you very much! Now Works!

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.