Ahmet Tanakol
Ahmet Tanakol

Reputation: 899

using a string in javascript from a template.html

I was wondering how I can assign a string from template.html to a javascript variable. I'm using a php code that transforms rss to html. It replaces strings in a template.html in order to display information. I want to assign these replaced strings in javascirpt. Here my codes:

My template.html

 <TABLE width="100%">
        ~~~BeginItemsRecord~~~
        <TR>
            <TD>
                 ~~~ItemPubShortTime~~~
            </TD>
        </TR>
        <TR>
            <TD>
                <B>~~~ItemTitle~~~</B>
            </TD>
        </TR>
        <TR>
            <TD>
                <BR>
            </TD>
        </TR>
        ~~~EndItemsRecord~~~
    </TABLE>

When I run my program, for example instead of ~~~ItemPubShortTime~~~ you see time like 10.16AM. I want to assign 10.16 AM to a javascript variable in this file.

My script looks like this:

<script language="javascript">
var sglm=new Array();
sglm[0]= 'Hello World';

</script>

I want to put 10.16AM instead of 'Hello World'. I hope it is clear. Thank you.

Upvotes: 0

Views: 87

Answers (3)

Moritz Roessler
Moritz Roessler

Reputation: 8611

You could run a loop over the td fields and write the textContent in the Array,

var sglm = [];
for (var i = 0, ilen = document.getElementsByTagName("TD").length; i < ilen; i++ ) {
    sglm.push(document.getElementsByTagName("TD")[i].textContent);
}
console.log(sglm[0]); //~~~ItemPubShortTime~~~​​​

See this Fiddle

If you don't have a console replace console.logwith an alert or sth

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

You can put the JavaScript in the template as well, then assign it same way via the PHP code e.g.

   sglm[0]= '~~~ItemPubShortTime~~~';

Then it will be replaced along with the HTML.

Upvotes: 1

M Abbas
M Abbas

Reputation: 6479

what about:

<TD id="theId">~~~ItemPubShortTime~~~</TD>

<script language="javascript">
var sglm=new Array();
sglm[0]= document.getElementById("theId").textContent;

</script>

Upvotes: 0

Related Questions