Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use html variables in javascript function call?

Status
Not open for further replies.

Poseidon1003

Programmer
Apr 17, 2012
4
0
0
US
Hi I'm newbie in this area.

How can I use html variables in javascript function embraced in html?
Here's my code "sitelist.html".

<body>
<div class="content">
<table id="rptmenus" width="78%" height="50" border="0">
<tr> <th> Site Settings </th> </tr>
<tr> <td><p>You are currently logged in as: {{user.username}} </p></td> </tr>
<tr class="alt"> <td> Site/Location information file (text or csv) </td> </tr>
<tr> <td><p>File opened: {{filename}} </p></td> </tr>
</table>

<table id="sites" width="78%" border="0">
<tr> <script type="text/javascript">loadSite(filename)</script> </tr>
</table>
</div>
</body>

This html is called by
render_to_response('sitelist.html', {'user':user, 'filename':filename})
in "views.py".

In html code, I'm going to call "loadSite" function with "filename" variable from render_to_response.
{{filename}} in html displays desired filename, but "loadSite(filename)" didn't work properly.

How can I call "loadSite" with {{filename}} variable?
 
javascript runs (typically) client-side. unless you are asking as asp question.

I am not sure what you mean by an html variable. do you mean a server-side include? or are you using some other kind of server-side parser? I'd guess python.

whatever the case, I assume the variable is parsed server side, so that it is served as text to the browser.

in which case, since javascript runs at the browser, the 'variable' is irrelevant. it is just text.

I don't understand your statement "This html is called by ...". That doesn't make sense to me. HTML is not 'called'. it is just text. So I choose to interpret your statement as 'the html is generated as a result of a call to the render_to_response() function'.

you say this function is in views.py. however it looks[\i] like a javascript function. or at least a function that takes a javascript object as an argument. python can accept variables expressed in JSO notation (commonly JSON) so i can't be sure what this function actually is.

ideally you'd post the code of loadSite() and views.py.

but as a quick fix and wild stab in the dark, you might try adding this to the top of the file (above the <body> tag)
Code:
<script type="text/javascript">
var filename='{{filename}}';
</script>
 
Thanks Jpadie.
Sorry for using improper words in the question.
Anyway, your reply code is just what I wanted. It works.

I spent couple of hours on googling to find this simple solution.

Thanks so much.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top