Reputation: 327
How can I pass a tag`s value to a javascript function?
this works:
onclick="submit(this.value)"
but this dosnt work:
onclick="submit(document.getElementById("ShortcutID").value)"
Upvotes: 0
Views: 277
Reputation: 7722
The problem you have in your code is you are using two sets of double quotes. To fix this, try this: onclick="submit(document.getElementById('ShortcutID').value)"
Upvotes: 0
Reputation: 995
there is syntax error.
ShortcutID should be enclosed in this 'ShortcutID'
Change this
onclick="submit(document.getElementById("ShortcutID").value)"
to this
onclick="submit(document.getElementById('ShortcutID').value)"
Upvotes: 1
Reputation: 1075209
but this dosnt work: onclick="submit(document.getElementById("ShortcutID").value)"
Because you're using double quotes both to delimit the onclick
attribute and also inside to delimit the JavaScript string. Try:
onclick="submit(document.getElementById('ShortcutID').value)"
This is why JavaScript allows both single and double quotes for quoting strings.
You can also do it with entities, because remember that the content of an attribute is HTML text just like anything else in the HTML, so:
onclick="submit(document.getElementById("ShortcutID").value)"
The fact that the content is HTML text tends to be problematic when you're doing something non-trivial, which is one reason not to use onclick="code"
style event handling but rather hooking up the event in code instead.
Upvotes: 0
Reputation: 944171
If you want to include quote characters in an attribute value delimited with the same kind of quote characters, you have to represent them with character references.
onclick="submit(document.getElementById("ShortcutID").value)"
Alternatively, use a different kind of quote character.
onclick="submit(document.getElementById('ShortcutID').value)"
… but try to avoid using intrinsic event attributes and bind your JavaScript event handlers with JavaScript instead. See Unobtrusive JavaScript
Upvotes: 1