Jules Hummelink Jules Hummelink - 1 year ago 99
HTML Question

JS replace id from html in var

I have created a javascript that injects pieces of html in a index.php file. It needs to replace the id with a new id with a index number in it.
This is my type.txt:

<section id="banner" class="wow fadeInUp" style="position: relative; padding: 100px 0;">
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6">
<div class="block" style="padding-top: 10px;">
<h1 id="K0title" style="margin-bottom: 18px; line-height: 55px; color: #262b31; font-weight: 300;">
<p id="K0text" style="margin-bottom: 65px; font-size: 20px; color: #999; font-weight: 300;">

<div class="col-md-4 col-sm-6">
<div class="block" style="padding-top: 10px;">
<img id="K0image" class="app-img img-responsive" src="images/background 2.png" alt="">


And this a piece of my javascript:

if(type == 1){
var html1 = new XMLHttpRequest();
html1.open('GET', 'js/txt/type1.txt', false);
allText = html1.responseText;
titleReplace = "K" + id + "title";
textReplace = "K" + id + "text";
imageReplace = "K" + id + "image";
allText.replace("K0title", titleReplace);
allText.replace("K0text", textReplace);
allText.replace("K0image", imageReplace);
document.getElementById(id).innerHTML = allText;
document.getElementById(titleReplace).innerHTML = title;
document.getElementById(textReplace).innerHTML = text;
document.getElementById(imageReplace).src = imageUrl;

Somehow, the id stays K0... in the index.php.

I think that is has something to do with the ""


Answer Source
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");

Check this link:-http://www.w3schools.com/jsref/jsref_replace.asp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download