2

I've been working now for a while on the javascript process and can't really figure out on how to do this. I know how to basically tell javascript to set a certain value for a specific ID. But my Problem here is: I'd like to write javascript to read out information (Server based, but that I'll do by myself ofc.) set it as var and then write it into text. It should look like this in the end:

Before writing (Without Javascript):
<h1 id="name" class="normal">Welcome, %Name%! What do you wish to do?</h1>

After writing (With Javascript):
<h1 id="name" class="normal">Welcome, John Connor! What do you wish to do?</h1>

So basically what I thought of is, that JS reads out information from server, then finds the h1 ID, searches in that text the %Name% value, and replaces it with the found name.

Please help me out here.Thanks!

Best Regards Kodaigan

Shambhavi
  • 305
  • 1
  • 14
Kodaigan
  • 85
  • 1
  • 10
  • so what's the question? what have you tried? Seems to me like you want something like http://handlebarsjs.com/ – atmd Aug 07 '15 at 07:51
  • Specify your question to be more clear for understanding. – Brane Aug 07 '15 at 07:54
  • I've tried many things, but the only things I was able to do until now was editing the h1 completely, I just want to edit a specific part. I was searching for a solution since the 5th of August. (haha) How do I tell javascript to only edit a **specfic** part of a h1 by variables? – Kodaigan Aug 07 '15 at 07:55
  • your requirement is little unclear.. the list of keyvalue pair you are refering here will it come from server side??? – yeppe Aug 07 '15 at 07:58
  • Forget the server sided part. As I said I will manage that by myself. I need help with javascript telling him what exactly to edit. As example: You have a long text, and you only want to edit something in between without changing the text around it. So: Javascript reads out that specified value and replaces it automatically. – Kodaigan Aug 07 '15 at 07:59
  • To make things clear the code I have now looks like this: `` Which refers (in body) to: `

    Welcome, %Name%! What do you wish to do?

    ` I'd like to know on how to rewrite the js into something which does not replace the entire value of h1.
    – Kodaigan Aug 07 '15 at 08:07
  • Like atmd mentoined. Take a look at [Handlebars.js](http://handlebarsjs.com) – Sebastian Aug 07 '15 at 08:18
  • Sorry, I cannot do much with handlebars or embeddedjs. I'm just too green behind the ears to understand the usage of those things. Anyway, I've been experimenting a bit with an idea which looks now like this: `window.onload = function test () { var namevalue = "ServerName"; var replacename = document.getElementById("name").value = "%Name%"; namevalue = replacename; return false; }` What is missing? I think this is not a bad start....right? – Kodaigan Aug 07 '15 at 08:32

2 Answers2

5

If using jQuery:

$('#name').html(function(index, html){
    return html.replace('%Name%', yourVariable);
});

Pure javascript:

document.getElementById('name').innerHTML = document.getElementById('name').innerHTML.replace('%Name%','John');

This will replace a given string inside your h1 element.

Shambhavi
  • 305
  • 1
  • 14
  • I like your suggestion, but I can't make it work. I know I'm doing something wrong. Mind helping me here? [JSFiddle](https://jsfiddle.net/#&togetherjs=hwkavh0xAI) – Kodaigan Aug 07 '15 at 08:46
  • @Kodaigan Check [this](https://jsfiddle.net/s0udrxkv/) fiddle I created referring your code. Let me know if you still face problems. Also, it would be better if you show your code. – Shambhavi Aug 07 '15 at 08:56
  • Oh. _That_ Variable. I thought I could also put in there a set Variable. Anyway, thanks man! It helped alot! :) – Kodaigan Aug 07 '15 at 09:07
  • Hey Shambhavi, something went wrong. In my testhtml everything went fine. But when I tried to implement in into my actual html, it doesn't change anything. (since you've asked for the complete code, [Here you go](https://jsfiddle.net/zb65aokw/). Since there are extern css, the result looks a bit messy. – Kodaigan Aug 07 '15 at 09:35
  • @Kodaigan I have edited my answer. Looks like you are not using jQuery, refer the second option. Let me know if it doesn't work. Also, I recommend you use [jQuery](https://jquery.com/). – Shambhavi Aug 07 '15 at 10:01
  • Purrrrrfect! I just added some "window.onload" and it worked as if it was magic! Thanks again! – Kodaigan Aug 07 '15 at 10:56
0

If you have many paramaters, is better to use one of js lib's with templating.I prefere to use underscore. http://underscorejs.org/#template. Also you can use if statements in youe template How to use if statements in underscore.js templates?

Community
  • 1
  • 1
Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37