Showing and adding text to a p element with jquery

jquery add text to div
jquery append multiple elements
append after jquery
jquery get span text
jquery create element
jquery replace text
jquery replace text in div
append js

Trying to show a p element when a successful event has happened in my program. But i can only get the p element to appear but cannot get the text to show in the element, where am i going wrong?

$('.success-msg').show().append('<strong>Success:</strong> Your review has been created');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="success-msg"></p>

Use this

$('.success-msg').html('<strong>Success:</strong> Your review has been created').show();

.append(), Try this $('p').append('<span id="add_here">new-dynamic-text</span>');. OR if there is an existing span, do this. How to get the text inside an element using jQuery. Topic: JavaScript / jQuery Prev|Next. Answer: Use the jQuery text() method. You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.

Resolve the returned Promise using a $.when() statement

var show=function(){
    return $('.success-msg').show();
}
$.when(show()).done(function(){
    $('.success-msg').append('<strong>Success:</strong> Your review has been created');
});

Another way to solve it is

$('.success-msg').show(function(){
    $(this).append('<strong>Success:</strong> Your review has been created')
});

Another way to solve it is

$('.success-msg').html('<strong>Success:</strong> Your review has been created').show();

Your main mistake is that you put you script tag first so at the time of loading function there is no p with that class for that reason either you have to put script tag before end of body or encapsulated your code with in $(document).ready()

jQuery append text inside of an existing paragraph tag, jQuery text() Method. ❮ jQuery HTML/CSS Methods. Example. Set text content for all <p> elements: $("button")  The jQuery prepend () method inserts content AT THE BEGINNING of the selected HTML elements. $ ("p").prepend("Some prepended text."); Try it Yourself » Add Several New Elements With append () and prepend () In both examples above, we have only inserted some text/HTML at the beginning/end of the selected HTML elements.

Your code is correct, thus I don't suggest new ways.

But, wrap your code in document.ready. It is possible that the p element is visible always and showing it is not because your code. Code below is full and testable without any changes:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <p class="success-msg"></p>
 <script>
     $(function() {
        $('.success-msg').show().append/*or .html(..) or .append($("<strong ..."))*/('<strong>Success:</strong> Your review has been created');
      });
</script>

Check your wrapped code online

jQuery text() Method, jQuery Effects. jQuery Hide/Show jQuery Fade jQuery Slide jQuery Animate jQuery stop() jQuery Callback jQuery Chaining We will look at four jQuery methods that are used to add new content: append() $("p").prepend("Some prepended text."); The elements are created with text/HTML, jQuery, and JavaScript/DOM. If you want to append text or html to span then you can do it as below. $('p span#add_here').append('text goes here'); append will add text to span tag at the end. to replace entire text or html inside of span you can use .text() or .html()

jQuery Add Elements, jQuery provides several methods, like append() , prepend() , html() , text() will insert a <h1> , <p> and an <img> element inside the <body> element as a last  Use append() to add text/html to an element with jQuery jQuery’s function append() can be used to add text or html to an element. It’s very easy to use and this post shows some example code and has a working example where you can type in some text that will be appended to a div.

jQuery Insert Content Inside, Before or After an Element, The article has an example that explains how to add text to div element using jQuery. .com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> </head> <body> <p​>Click All I wanted to show here is how to add text to pre-defined or dynamically  jQuery – Search text in the Element with :contains() Selector The :contains() is a jQuery selector which allows us to search text within the element and select it and perform an action on it. For example – you are dynamically listing records from MySQL database table here, you have a requirement to add search filter based on key press.

How to add text ao a DIV using jQuery .appendTo() and .append , JavaScript Code : $( "p" ).add( "<span>Exercises</span>" ).appendTo( document​.body );. The .text () method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val () method. To get the value of a script element, use the .html () method. As of jQuery 1.4, the .text () method returns the value of text and CDATA nodes as well as element nodes.

Comments
  • the code works well ? what version of jquery you're using poste the whole used code heere ?
  • I tried it as well, and it works. Maybe, it could be the jQuery version.
  • Version 3.3.1 ?
  • Check console for error
  • using the .text, the strong element will literally be printed.
  • Sory, I can not understand your comment
  • The OP's code work in snippet but you posted new code
  • No the OP’s code in this way is not work he miss the difference between syncchronous and asynchronous so he will not find the idea of why his code is not work while he wrote it in a good way
  • This code brings back the same result as the original ?
  • i add two codes block now you have three you can check them if they are working or not then choose the one suitable for you