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>    
Henrikh
  • 150
  • 2
  • 3
  • 11
  • `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). – Felix Kling Sep 11 '11 at 08:12
  • I edited, please look my last line I added in my question. how to show the real variable "theValue" instead of "variable" ? – Henrikh Sep 11 '11 at 08:25
  • Marco's answer will work, you can just add `obj.innerHTML = "number " + theValue;` to your function: http://jsfiddle.net/SJdea/ – Felix Kling Sep 11 '11 at 08: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 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'; ? – Henrikh Sep 11 '11 at 08: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 http://stackoverflow.com/questions/6170858/php-variable-inside-a-js-file – Felix Kling Sep 11 '11 at 08:37
  • I got, I'll do vice-verse, thanks a lot guys... You helped me a lot. I cannot vote as I don't have the minimal 15 reputations to vote. – Henrikh Sep 11 '11 at 08:39

3 Answers3

1

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

AlexB
  • 726
  • 4
  • 13
  • @Daniel: The problem is that `document.writeln(theValue);` is executed before you ever call `showHide` (I assume). Maybe you can create a http://jsfiddle.net/ demo. – Felix Kling Sep 11 '11 at 08:24
  • -1 for not reading more than the title. The actual problem is different. – Felix Kling Sep 11 '11 at 08:39
  • @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. – AlexB Sep 11 '11 at 08:50
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
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
  • maybe You are right, but how to use it? Set this code in my function and it'll display in the "toShow" div part? – Henrikh Sep 11 '11 at 08:19
  • @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. – Felix Kling Sep 11 '11 at 08:21
  • sorry that I didn't do it before. Now I edited my question and in the last line I added my HTML part – Henrikh Sep 11 '11 at 08:27
  • 1
    @Daniel: So what's the problem now? The solution here works: http://jsfiddle.net/SJdea/ – Felix Kling Sep 11 '11 at 08:33
  • @David: Then please accept this answer by clicking at the tick outline next to it. – Felix Kling Sep 11 '11 at 08:38
0

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

Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58