-1

I need to change a text content inside a contact form using jQuery, I know that the custom.js file that I'm using is connected as when I do a console.log I can see what I write.

When I select the paragraph that I need and change it's html content nothing happens, is it possible for the content to be overwritten by something later? Is there a way to prioritize my code?

jQuery("p.help-block.small").html("<p> ... </p>");

3 Answers 3

2

In some instances, I'll run a delayed init function in a setTimeout like this:

var delay = setTimeout(function() {
    jQuery("p.help-block.small").html("<p> ... </p>");
}, 100);

In the event that something is overwriting the html of that paragraph block on page load, this function will run 100 milliseconds after that, making yours the last change to the paragraph. 100 milliseconds is rather quick, so with other elements on the page loading, users may or may not notice the change.

I generally use this type of load with the elements hidden via CSS, and then fading them in when they've been "loaded" if there are a lot of elements that will will change in shape or size, e.g. going from three words to three paragraphs would make the page jump, so I may fade it in nicely instead.

Also, since you're calling .html() on the paragraph element, you don't need to include the paragraph tags within since the html you would be setting is already wrapped by paragraph tags. If you're just changing text, you could use .text() instead. However, if you're including line breaks or span elements within the paragraph, then .html() would be appropriate.

5
  • There has to be something else going on as this doesn't work either and it should.
    – Sergi
    Mar 27, 2017 at 17:50
  • 1
    If this doesn't work, then I'm starting to think the issue is with your selector. With the page open, press the F12 key to open the debugger and click "Console" along the top (if you don't see it, press the little arrow keys ">>" to see additional screens and it should be listed there). In the console, copy/paste the function in your original post. If it STILL doesn't change, then remove ".html(" and everything after that. If you see just "[]" in the console, then your selector is definitely incorrect.
    – AuRise
    Mar 31, 2017 at 14:23
  • Based on your original code, you are targeting a paragraph element that has the classes "help-block" and "small" on it.
    – AuRise
    Mar 31, 2017 at 14:26
  • I'll accept your answer as this last comment helped me a lot, it was a complicated selector and that helped me a lot. I did get an empty array as a result of my query.
    – Sergi
    Apr 1, 2017 at 8:05
  • Good to know! You've no idea the countless hours I've spent debugging issues like this only to find out the code was correct, but the selector was wrong, haha.
    – AuRise
    Apr 3, 2017 at 13:57
1

Have you tried to wrap it into the jQuery() function?

jQuery(function(){
  jQuery("p.help-block.small").html("<p> ... </p>");
})

This will cause the code to run one the page is loaded, so that every script has already been parsed&run.

1
  • It should but it's not working.
    – Sergi
    Mar 27, 2017 at 17:48
0

You could try calling the "custom.js" file at the bottom of your page. This means the HTML and CSS should load first then will get overwritten by the script. You could also do a window.alert() to test that your code inside of JQuery is running correctly.

Not the answer you're looking for? Browse other questions tagged or ask your own question.