Reputation: 402
I am trying to make a img that when it is clicked a JavaScript function is called.
I have searched on the web but haven't found anything that actually works (prob because of a mistake I made).
This code was made to pass JavaScript variables to a c# application.
Can anybody tell me what I am doing wrong?
<script type="text/javascript">
function exportToForm(a,b,c,d,e) {
window.external.values(a.value, b.value, c.value, d.value, e.value);
}
</script>
</head>
<body>
<img onclick="exportToForm('1.6','55','10','50','1');" src="China-Flag-256.png"/>
<button onclick="exportToForm('1.6','55','10','50','1');" style="background-color: #00FFFF">Export</button>
</body>
Upvotes: 20
Views: 242014
Reputation: 3508
In response to the good solution from maček. The solution didn't work for me. I have to bind the values of the datas to the export function. This solution works for me:
function exportToForm(a, b, c, d, e) {
console.log(a, b, c, d, e);
}
var images = document.getElementsByTagName("img");
for (var i=0, len=images.length, img; i<len; i++) {
var img = images[i];
var boundExportToForm = exportToForm.bind(undefined,
img.getAttribute("data-a"),
img.getAttribute("data-b"),
img.getAttribute("data-c"),
img.getAttribute("data-d"),
img.getAttribute("data-e"))
img.addEventListener("click", boundExportToForm);
}
Upvotes: 0
Reputation: 77778
You should probably be using a more unobtrusive approach. Here's the benefits
- Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation
- Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
- Progressive enhancement to support user agents that may not support advanced JavaScript functionality
Your JavaScript
function exportToForm(a, b, c, d, e) {
console.log(a, b, c, d, e);
}
var images = document.getElementsByTagName("img");
for (var i=0, len=images.length, img; i<len; i++) {
img = images[i];
img.addEventListener("click", function() {
var a = img.getAttribute("data-a"),
b = img.getAttribute("data-b"),
c = img.getAttribute("data-c"),
d = img.getAttribute("data-d"),
e = img.getAttribute("data-e");
exportToForm(a, b, c, d, e);
});
}
Your images will look like this
<img data-a="1" data-b="2" data-c="3" data-d="4" data-e="5" src="image.jpg">
Upvotes: 6
Reputation: 519
Well your onclick function works absolutely fine its your this line
window.external.values(a.value, b.value, c.value, d.value, e.value);
window.external is object and has no method name values
<html>
<head>
<script type="text/javascript">
function exportToForm(a,b,c,d,e) {
// window.external.values(a.value, b.value, c.value, d.value, e.value);
//use alert to check its working
alert("HELLO");
}
</script>
</head>
<body>
<img onclick="exportToForm('1.6','55','10','50','1');" src="China-Flag-256.png"/>
<button onclick="exportToForm('1.6','55','10','50','1');" style="background-color: #00FFFF">Export</button>
</body>
</html>
Upvotes: 0
Reputation: 22480
Put the javascript part and the end right before the closing </body>
then it should work.
<img onclick="exportToForm('1.6','55','10','50','1');" src="China-Flag-256.png"/>
<button onclick="exportToForm('1.6','55','10','50','1');" style="background-color: #00FFFF">Export</button>
<script type="text/javascript">
function exportToForm(a,b,c,d,e) {
alert(a + b);
window.external.values(a.value, b.value, c.value, d.value, e.value);
}
</script>
Upvotes: 1
Reputation: 3680
This should work(with or without 'javascript:' part):
<img onclick="javascript:exportToForm('1.6','55','10','50','1')" src="China-Flag-256.png" />
<script>
function exportToForm(a, b, c, d, e) {
alert(a, b);
}
</script>
Upvotes: 41