Solved

Why doesn't javascript remove comma?

Posted on 2016-08-22
2
43 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 51

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 51

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now