0

I need to display JS variable in HTML.

<script language="JavaScript"> function showHide(toShow,toHide,theValue) { obj = document.getElementById(toShow); obj2 = document.getElementById(toHide); obj.style.display = 'block'; obj2.style.display = 'none'; } </script> 

but I need to display "theValue" in HTML. I heard about this:

<script type="text/javascript"> document.writeln(theValue); </script> 

But this "theValue" is not global variable. How can I make it global variable? (out of function).

<table><tr><td onMouseOver="showHide(2,1,67);">sss</td></tr></table> <div id="2" style="display:none;"> number "variable" </div> 
6
  • document.writeln should only be executed while the document is being parsed. showHide seems to be called after the document was parsed, so you shouldn't use it anyway. Where exactly do you want to output theValue? You can just get a reference to that element and set its innerHTML (inside showHide of course). Commented Sep 11, 2011 at 8:12
  • I edited, please look my last line I added in my question. how to show the real variable "theValue" instead of "variable" ? Commented Sep 11, 2011 at 8:25
  • Marco's answer will work, you can just add obj.innerHTML = "number " + theValue; to your function: jsfiddle.net/SJdea Commented Sep 11, 2011 at 8:27
  • yes guys You are correct, that's working like I want. that' cool. but got 2 questions about that. 1. how to add html part like <font size="4"> in that code? And 2nd question is how to compare with php variable? Like if "theValue" is higher 7 echo 'not enough'; else echo 'enough'; ? Commented Sep 11, 2011 at 8:34
  • First, the font element is deprecated, use CSS instead. Second, you cannot just mix JavaScript and PHP. PHP is run on the server and JavaScript on the client. If your PHP script is generating the page, you can set the value in JavaScript though. See stackoverflow.com/questions/6170858/… Commented Sep 11, 2011 at 8:37

3 Answers 3

1

Just use window.theValue instead, and it will be available in the global scope

Sign up to request clarification or add additional context in comments.

3 Comments

@Daniel: The problem is that document.writeln(theValue); is executed before you ever call showHide (I assume). Maybe you can create a jsfiddle.net demo.
-1 for not reading more than the title. The actual problem is different.
@Felix I actually read more than the title, but truth be told i did fail to read the last paragraph of the question so for that very reason i am not going to argue about the downvote. Please accept my apologies.
1

You are probably looking for

obj.innerHTML = theValue; 

or whatever commodity function your framework is providing. Are you using jQuery, YUI, or some other library?

As for global variables, the usual suggestion is not to use them at all, if possible.

But if you absolutely need some global paramer, you can make a single object (outside any function, at the start of your page) like

var MYNS = {}; 

and create your objects inside it, to keep the global variable space as clean as possible:

MYNS.theValue = 42 

5 Comments

maybe You are right, but how to use it? Set this code in my function and it'll display in the "toShow" div part?
@Daniel: Have you tried it? We cannot really help you more, because you did not specify where you want to output the value. For all we know, you are calling some function and want to print a value somewhere. IF you explain us what your code is supposed to do, then we can be more helpful.
sorry that I didn't do it before. Now I edited my question and in the last line I added my HTML part
@Daniel: So what's the problem now? The solution here works: jsfiddle.net/SJdea
@David: Then please accept this answer by clicking at the tick outline next to it.
0

You can just avoid prefixing the variable with the var keyword.

1 Comment

I don't understand can You bring an example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.