> I'm able to output a global variable value like this:
> <p>The value of myGlobalVariable is <script>myGlobalVariable</script>.</p>
 
Huh? What browser does that work in? HTML isn't a templating language, and a
<script> tag doesn't do a text replacement of the script's return value, it
just runs the script. You didn't actually get this to work, did you?
 
> I would like to be able to do something as simple as
> <p id="<script>myGlobalVariable</script>">xyz</p>,
> but apparently the "" marks are a problem.
 
Whoa cowboy, that's even farther from anything you could ever actually do:
you're trying to nest an HTML tag inside the attribute of another tag.
 
Instead, the way you do stuff like this is to write JavaScript code that
generates the HTML or DOM elements.
 
For example, during page loading (not in a document ready function), you can
use document.write():
 
<script type="text/javascript">
    // myGlobalVariable has been previously defined
    document.write( '<p id="', myGlobalVariable, '">xyz</p>' );
</script>
 
That works with local variables as well, of course.
 
<script type="text/javascript">
    (function() {
        var foo = someFunction();
        document.write( '<p id="', foo, '">xyz</p>' );
    })();
</script>
 
Or, in jQuery you can do things like this:
 
<script type="text/javascript">
    $(function() {
        $('#someContainer').html(
            $('<p>xyz</p>').attr({ id: myGlobalVariable })
        );
    });
</script>
 
Alternatively, there are several JavaScript-based template systems, from the
very simple to the rather complex. They may let you code in a style closer
to what you're hoping to use.
 
-Mike

  _____  

From: Rick Faircloth


I would like to be able to do something as simple as

<p id="<script>myGlobalVariable</script>">xyz</p>,

but apparently the "" marks are a problem.

 

I'm able to output a global variable value like this:

<p>The value of myGlobalVariable is <script>myGlobalVariable</script>.</p>

 

Is there some way to use global variable values with an id attribute?

 

Thanks,

 

Rick

 

----------------------------------------------------------------------------
-----------------------------------------------------------

"It has been my experience that most bad government is the result of too
much government." - Thomas Jefferson

Reply via email to