Hello,
Is it possible to access a JavaScript variable and use its value as an attribute for a custom tag? For example:
<script>
var test = "test"
<mytag:test attr="WANT TO USE VALUE OF test HERE" />
</script>
Thanks in advance!
Printable View
Hello,
Is it possible to access a JavaScript variable and use its value as an attribute for a custom tag? For example:
<script>
var test = "test"
<mytag:test attr="WANT TO USE VALUE OF test HERE" />
</script>
Thanks in advance!
You could do something like this:
<script>
test = "a test-value";
document.all.myid.setAttribute("href", test, true);
</script>
<a href="" id="myid">...</a>
Hi Susad,
Thanks for the reply. When I tried it your way I got the
following error: document.all.myid is null or not an object.
I have tried everywhere to get an answer for this but
without success.
Regards
lerouxjcg
Yes, thanks to all for the replies. I too get an error when I try the method suggested by Susad. I think that the basic problem is that what I want to do is mix server-side data (custom tags and java scriptlets) with client-side data (JavaScript) and it's not possible to do that dynamically. I ended up just using a form and post method to reload the page content from the server.
Thanks again for all the ideas.
You got the "document.all.myid is null or not an object" error msg because the a-Tag (or whatever document.all.myid should be) still isn't loaded. You should encapsulate this into a function like this:
In this example you call the function func. And this function checks whether the a-Tag exists (that means it checks whether the document is loaded complete). If not it calls itself after a timeout (500 ms in this case) until document.all.myid is available.Code:<script>
function func(value)
{
if (document.all.myid != null)
document.all.myid.setAttribute("href", value, true);
else
setTimeout("func("+value+");", 500);
}
func("something");
</script>