Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why doesn't javascript remove comma?

Posted on 2016-08-22
2
Medium Priority
?
92 Views
Last Modified: 2016-08-22
On this page http://www.bitesizebschool.com/copywriting/, I have the following javascript just below the body tag:

<script>
var cite = document.querySelector("#le_body_row_4_col_1_el_2 cite");
cite.innerHTML = cite.innerHTML.replace(/Source:,/,"Source:");
</script>

Open in new window


I want to remove the comma in the first quote below the "start here" button: "Source:,". However, nothing is happening. Does anyone see what I'm doing wrong?
0
Comment
Question by:brettr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41765334
The problem is because the script is running before the HTML has been rendered. Place the script in a document ready block
JQuery
$(function() {
  var cite = document.querySelector("#le_body_row_4_col_1_el_2 cite");
  cite.innerHTML = cite.innerHTML.replace(/Source:,/,"Source:");
});

Open in new window

0
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41765345
To confirm F12 to view the console  you should see an undefined error
TypeError: cite is null
   cite.innerHTML = cite.innerHTML.replace(/Source:,/,"Source:");
/copywriting/ (line 134, col 1)

Open in new window

In case $ is not defined
jQuery(function() {
  var cite = document.querySelector("#le_body_row_4_col_1_el_2 cite");
  cite.innerHTML = cite.innerHTML.replace(/Source:,/,"Source:");
});

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question