Faqts : Business : Programming : Shopping For You : JavaScript : DHTML : DOM

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

183 of 312 people (59%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How to use javascript variable in a static HTML

Aug 24th, 2009 07:48
Joe Bloggs, chat alarab, Klaus Bolwin, Mark Filipak, Rotti Sowmindra,


Rotti:
I have a div tag,which stores some HTML code like this
<div id=div1><input name=textBox id=text1></div>
When ever i access the above text as div1.innerHTML i need the id of 
the text field to be incremented.

Mark:
Sounds like you are trying to implement a text box stack such that only
one text box is visible at any one time. When the vistor has filled in
the first box, he/she is presented with a second box that overlays the
first box. After filling in the second box, the visitor is presented
with a third box, and so on through all the boxes. Is that it?

Rotti:
I have used it like this
<div id=div1><input name=textBox id="javascript:eval("text"+counter)
></div>
but it doesnot seem to be working.
Many suggest that i can do the same in JavaScript,but i have a 
different scenario where i dont want to use javascript,i want to 
javascript variable inside div tag and it has to be updated everytime i 
access it.

Mark:
Well, the "javascript:..." statement that you have above will not work,
even if it was syntactically correct. More importantly, it *is*
javascript. You write that you "dont want to use javascript", but you
are using javascript. ...Okay, some education:

The "javascript:" notation is a protocol, just like "http:" and "ftp:"
are also protocols. It can only be used where the browser expects a URL.
Thus, "javascript:alert('Hello!');" is equivalent to
"http://www.yahoo.com" except that instead of contacting Yahoo using the
http protocol, the browser launches the javascript interpreter and gives
it "alert('Hello!');" to evaluate. So when you write this:

  Click <a href="javascript:alert('Hello!');">here</a> for a greeting.

it works the same as this:

  Click <a href="http://www.yahoo.com">here</a> to go to Yahoo.

In both cases, when the browser encounters 'href=', it expects a URL.
But when the browser encounters 'id=' it expects a string, not a URL.
Putting a URL in the place where the browser expects a string doesn't
work because the browser treats it as a string, not a URL.

Rotti:
I hope you got my problem.
Thanks
Rotti Sowmindra

Mark:
I think what you want to do is implement the stack of text boxes I
outlined above. If so, here's how you might do it (but it is still using
javascript, just hidden javascript):

<body onload="document.getElementById('myInput1').focus();">
<div style='position:relative;'>
  <div id='myDiv1' style='position:absolute;visibility:visible;'>
    This is box 1. Please fill it in:
    <input id='myInput1' type='text' value=''
     onkeypress="if (event.keyCode==13){
     document.getElementById('myDiv1').style.visibility='hidden';
     document.getElementById('myDiv2').style.visibility='visible';
     document.getElementById('myInput2').focus();}">
  </div>
  <div id='myDiv2' style='position:absolute;visibility:hidden;'>
    This is box 2. Please fill it in:
    <input id='myInput2' type='text' value=''
     onkeypress="if (event.keyCode==13){
     document.getElementById('myDiv2').style.visibility='hidden';
     document.getElementById('myDiv3').style.visibility='visible';
     document.getElementById('myInput3').focus();}">
  </div>
  <div id='myDiv3' style='position:absolute;visibility:hidden;'>
    This is box 3. Please fill it in:
    <input id='myInput3' type='text' value=''
     onkeypress="if (event.keyCode==13){
     document.getElementById('myDiv3').style.visibility='hidden';
     document.getElementById('myDiv4').style.visibility='visible';
     alert('Your box entries were:\n\
     '+document.getElementById('myInput1').value+'\n\
     '+document.getElementById('myInput2').value+'\n\
     '+document.getElementById('myInput3').value);}">
  </div>
  <div id='myDiv4' style='position:absolute;visibility:hidden;'>
    Thank you.
  </div>
</div>

Klaus:
the following code increments the id of the input-element and shows the
id as content of the text-field. It works in MSIE and Mozilla:

<html>
<head>
<title></title>
</head>
<body onload="newid = 1;">
<div id="div1">
<input id="text1" onclick="newid+=1; this.id = 'text'+newid;
this.value=this.id;">
</div>
</body>
</html>
http://www.ksa-123.com
http://www.ksa-2000.com
http://www.chat-kuwait.com
http://www.vip-kuwait.com
http://www.chat-3rb.com
http://www.vip-3rb.com
http://www.3rb-chat.com
http://www.vipgulf.com
http://www.chat-gulf.com
http://www.vip-gulf.com

======================================

http://www.attorneyslawyersdirectory.com
http://www.findattorneyslawyers.com
http://www.goodlawyersattorneys.com
http://www.professionallawyersattorneys.com
http://www.bestprofessionallawyers.com
http://www.americalawyersattorneys.com
http://www.availablelawyers.com
http://www.lawyersattorneyslist.com
http://www.lawyersattorneysfirm.com
http://www.originallawyers.com
http://www.lawyersgulf.com
http://www.lawyersband.com
http://www.lawyersdatacenter.com
http://www.lawyerstigers.com
http://www.lawcliff.com
http://www.tidylawyers.com
http://www.lawyersblock.com
http://www.californialawyersattorneys.com
http://www.chicagoattorneyslawyers.com
http://www.goodcalifornialawyers.com
http://www.lawyersattorneysfaqs.com
http://www.divorceattorneyslawyers.com
http://www.duiattorneyslawyers.com
http://www.personalinjurylawyerslist.com
http://www.injurylawyersattorneys.com
http://www.dwilawyersattorneys.com
http://www.healthbigboss.com
http://www.healthdohealth.com
http://www.healthcass.com
http://www.healthsilence.com
http://www.healthpicnic.com
http://www.blackmesothelioma.com
http://www.affordablelawyersattorneys.com
http://www.pickarticles.com
http://www.bloodyarticles.com
http://www.healthment.com
http://www.healthbyhealth.com
http://www.ehealthebooks.com
http://www.bestproductsofhealth.com
http://www.dietlosingweight.com
http://www.americanehealth.com
http://www.healthydepartment.com
http://www.medicalehealth.com
http://www.healthservicescenter.com
http://www.ehealthenews.com
http://www.ehealthdept.com
http://www.benefitshealthcenter.com
http://www.toplawyersattorneys.com 
http://www.healthxhealth.com
http://www.mesotheliomakiller.com
http://www.healthinhealth.com
http://www.booksyshop.com
http://www.mesotheliomaout.com
http://www.mesotheliomadown.com
http://www.qwesz.com
http://americanahost.com
http://www.healthfirewall.com
http://www.healthbodyguard.com
http://www.flathealth.com
http://www.computerstan.com
http://www.financestan.com
http://www.technologystan.com
http://www.zobab.com
http://www.businessian.com
http://www.tarabiza.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.hantira.com
http://www.mantofa.com
http://www.tantofa.com
http://www.fantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.uploadarticles.com
http://www.articlesfreedirectory.com
http://www.articlesxarticles.com
http://www.shikapika.com
http://www.ganazat.com
http://www.stakoza.com
http://www.stupidarticles.com
http://www.damima.com
http://www.halazona.com
http://www.best-cheap-web-hosting.com

======================================