Solved

Why doesn't javascript remove comma?

Posted on 2016-08-22
2
56 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
  • 2
2 Comments
 
LVL 54

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 54

Accepted Solution

by:
Julian Hansen earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding mobile browsers 9 66
Scroll 5 news at a time. 4 28
How to change the link of an image using md5 in php ? 3 33
Grunt script for Build Process 1 29
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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

832 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