Insert JavaScript code using Text shortcodes without using php

I'm using some javascript code to show post dates and comment number in my blog, but my blog doesn't support using php codes.

My JavaScript code

if(showpostdate==true){document.write('<span class="post-date">'+daystr+'</span>')}
if(showpostcomment==true){document.write('<span class="post-comment">'+commento+'</span>')}

My HTML code to call the JavaScript code is

<script>showpostdate = true;var showpostcomment = true;</script>

I want to change this JavaScript code so if I write this text in my text box

[date][comment]

it can show the html code

<span class="post-date">'+daystr+'</span>
<span class="post-comments">'+commento+'</span>

Answers


If you use the jQuery library something like this would work to replace the shortcodes:

$('textarea').change(function() {
  $this = $(this);
  // to be more efficient I would employ a test here that a shortcode exists before applying the replace functions
  var textInBox = $this.val();
  textInBox = textInBox.replace('[date]', date); // where date is your date var
  $this.val(textInBox.replace('[comment]', commentsCount)); // where commentsCount is your comments var
});

It won't create the span that you want to wrap each variable in because you cannot nest these inside a textarea. If you want to have the shortcode typed in a textarea and then nest the returned variables in something so that you can style them, you will need to employ some kind of html + css magic where you place a see-through textarea over a div and update the div contents whenever the textarea is updated. You can then put the span elements inside the div container, and style as you want.


Need Your Help

Ruby Canvas (GUI)

ruby user-interface canvas

I need draw some lines/circles/rect/etc from ruby into something like canvas.