1

I have tried:

<!--...-->
<script type="text/javascript">
  var myVar = "some string";
</script>
<!--...-->
<input name="&{myVar};" ... />
<!--...-->

But using FireFox, the name is set to the literal string: "&{myVar};" instead of the value of myVar...

UDPATE: I see this called JavaScript Entities and hasn't been supported for a long time - but there must be way if one can include JavaScript directly in events (I realize attributes are not events, so JavaScript probably isn't evaluated).. ?! It would be a lot nicer than hardcoding the value as it used elsewehere a lot..

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
markmnl
  • 11,116
  • 8
  • 73
  • 109
  • Not sure what you're trying to do. What you've posted doesn't make any sense. – Geuis Oct 27 '10 at 04:14
  • I am trying to: "use a JavaScript variable as a XHTML attributes value?".. – markmnl Oct 27 '10 at 04:15
  • Where did you learn you can insert JavaScript variables in XHTML width `&{}` syntax? – alex Oct 27 '10 at 04:18
  • University and here: http://www.javascriptkit.com/javatutors/entity2.shtml – markmnl Oct 27 '10 at 04:24
  • Lets boil the question down to stock: What goal are you actually trying to achieve? The best way to help you out is to understand exactly what you're attempting to achieve. – Geuis Oct 27 '10 at 04:37
  • I want a single variable holding the value as my other JavaScript algorithms often when looping through elements are looking for that variable name so I would rather not hardcode it but use a global variable so it is set in one place.. – markmnl Oct 27 '10 at 04:44
  • You have already declared it as a global variable though. var myVar = 'my value'; is already in the global scope. At any point in the operation of the page, any javascript that runs can reference myVar and get its value. I'm more than happy to chat with you tonight on IM if that would help. Email me at geuis.teses@gmail.com. – Geuis Oct 27 '10 at 05:28
  • Thanks but I still have to hardcode the input elements name as it is defined in HTML not in JavaScript (like my code above). – markmnl Oct 27 '10 at 05:56
  • Maybe im being too pedantic - thats only having the name coded in two places - still I prefer one! – markmnl Oct 27 '10 at 05:59

4 Answers4

4
var myInput = document.createElement('input');
myInput.setAttribute('name', myVar);

someContainElement.appendChild(myInput);
Phil
  • 157,677
  • 23
  • 242
  • 245
  • http://www.javascriptkit.com/javatutors/entity2.shtml also its in my lecture notes.. – markmnl Oct 27 '10 at 04:23
  • 5
    My friend, throw away your notes. And tell your professor/teacher to read a book made since 1996. Read the following page: http://www.javascriptkit.com/javatutors/entity3.shtml. "As mentioned, only Netscape 4 supports JavaScript entities." – Geuis Oct 27 '10 at 04:26
  • @Mrk Mnl See my [answer](http://stackoverflow.com/questions/4029920/how-to-use-a-javascript-variable-as-a-xhtml-attributes-value/4029935#4029935). – alex Oct 27 '10 at 04:26
0

I'm afraid you have to use Javascript to manipulate the html element. You can use jQuery to make this easier:

<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<input id="myInput" .../>
<script>
    var myVar = "value";
    $("#myInput").attr("name", myVar);
</script>
Xenofex
  • 611
  • 6
  • 17
  • 2
    Not tagged jQuery, so please don't provide a jQuery solution. – alex Oct 27 '10 at 04:20
  • but in html, wrong link before: http://www.javascriptkit.com/javatutors/entity2.shtml – markmnl Oct 27 '10 at 04:24
  • Well, why solving Javascript problems using a Javascript library is wrong? I can certainly use pure Javascript to access the attributes of a html node. Why can I use jQuery to ease my work? The thread owner looks like new to Javascript. In this he doesn't know that a library called jQuery. Is my introducing the jQuery library bad? Inappropriate? – Xenofex Oct 27 '10 at 06:13
0

Both Phil's and Xenoflex's ways, are the better way to go. They are actually doing the same thing just different ways, one using jQuery the other pure Javascript.

Alex's will work as well but then everything is hardcoded and somewhat less flexiable to future changes. The way Alex left his answer open you could print the string or assign it to a variable to be appended to a javascript object.

I think the nearest match to what you're looking for is:

<script type="text/javascript">
  var myVar = "some string";
</script>
...
<script type="text/javascript">
  document.write('<input name="' + myVar + '" ... />')
</script>

But as I said you should really take one of the first two approaches.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
joatis
  • 3,375
  • 1
  • 17
  • 13
  • 1
    As I said it's not advisable but it may be the closest match to what he thinks he's trying to do. For the record I wouldn't do it, just trying to help him connect the dots... – joatis Oct 27 '10 at 04:39
0

Here's what you need to do.

Start by never reading javascript tutorials from a Java site again. Different monsters entirely, plus the tutorials on the site you're referencing are horrible.

Second, get a copy of Javascript the Good Parts, http://www.amazon.com/dp/0596517742/ This book is perhaps the most useful book ever written (in my opinion) about the language itself. It doesn't say anything about dealing with DOM API's.

I encourage you to learn the language itself before getting too deep into javascript libraries like jQuery. It'll do you a load of good sooner rather than later.

Once you're at least somewhat familiar with the proper ways to use javascript as a language, start investing your time into one library or another. jQuery is probably the most used, well loved, and kindest library out there. It will make dealing with cross-browser crap SO much easier for you.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Geuis
  • 41,122
  • 56
  • 157
  • 219
  • Im not using that tutorial - I use MDC mostly - but i thought it was possible as that was had been taught - pity it no longer is supported.. – markmnl Oct 27 '10 at 04:35