Solved

Why doesn't javascript remove comma?

Posted on 2016-08-22
2
68 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 56

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 56

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

696 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