Solved

Why doesn't javascript remove comma?

Posted on 2016-08-22
2
81 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 57

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 57

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

717 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