admin管理员组文章数量:1200790
Why does this not work?
<input type="file" id="kmlFiles2" multiple onchange="function(){alert('why does this not work')}()">
chrome gives me an error, Uncaught SyntaxError: Unexpected token (.
Firefox tells me SyntaxError: function statement requires a name.
But this does work?
<input type="file" id="kmlFiles2" multiple onchange="alert('but this does work')">
/
I am asking because I was was trying to use and MVC framework that injects code into the onchange event.
Why does this not work?
<input type="file" id="kmlFiles2" multiple onchange="function(){alert('why does this not work')}()">
chrome gives me an error, Uncaught SyntaxError: Unexpected token (.
Firefox tells me SyntaxError: function statement requires a name.
But this does work?
<input type="file" id="kmlFiles2" multiple onchange="alert('but this does work')">
http://jsfiddle.net/ewzyV/
I am asking because I was was trying to use and MVC framework that injects code into the onchange event.
Share Improve this question asked Jun 5, 2014 at 20:38 doodersondooderson 5651 gold badge9 silver badges19 bronze badges 3- 1 jsfiddle.net/ewzyV/1 – adeneo Commented Jun 5, 2014 at 20:41
- ^ if you use a function that executes, for instance an IIFE it should work – adeneo Commented Jun 5, 2014 at 20:41
- ^ but then, what would be the point ? – adeneo Commented Jun 5, 2014 at 20:42
5 Answers
Reset to default 20onchange="(function(){alert('this should work')})()"
Its not a good practice to create inline functions.
I would suggest something like
<html>
<script>
function myFunction(){
alert('hey it works');
}
</script>
<body>
<input type ='file' id='kmlFiles2' onchange="myFunction();" />
</body>
</html>
you can also consider writing
function init(){
document.getElementById('kmlFiles2').onclick=function(){alert('this also works');};
}
window.onload=init;
you can't call the anonymous function from html element so you can achieve this by event handling like:
<input type="file" id="kmlFiles2" multiple>
<script>
document.getElementById("kmlFiles2").addEventListener("change", function(){
alert('This is working')
})
</script>
If you want to use code inside on***=""
tag
<input type="text" onchange="{
console.log('anything');
}"
You can just use {} brackets it be like a export default but you still can use this and events
With your code segment:
onchange="function(){alert('why does this not work')}()"
there is a basic problem. I would expect from https://www.w3schools.com/jsref/event_onchange.asp that the format for JavaScript is (instead):
object.onchange = function(){myScript};
With the onchange
event, the "object." is implicit already, so the expected correct form to use would be:
onchange= "function(){alert("Why does this not work?"); return;}"
The absence of the return;
statement rarely gives problems but - even so - I include it to make the function complete.
I am starting with this reference:
https://www.w3schools.com/js/js_function_definition.asp
After a function expression has been stored in a variable, the variable can be used as a function:
Example:
var x = function (a,b) {return a * b}; var z = x(4, 3);
I found that the following code is working:
onchange="{alert('Hello 10'); alert('Hello 20'); return; }"
It seems that function()
is implicit also. I tested it and the alert posted two times. I started out with just the single alert. Since it works for me, you might want to give this as a hint how it "should" work hopefully for you too.
After testing awhile with the inline functions, they seemed too unstable in order to get something consistently working. So I ended up with the code below that works solidly for me, simply defining a function to do what needs to be done:
<td><strong>Number of Posts For the Carousel to Show:</strong></td>
<td style="text-align: center; vertical-align: middle; padding: 10px;" colspan="2">
<input id="<?php echo esc_attr( $this->get_field_id( 'ceupoststoshow' ) ); ?>" style="vertical-align: middle; horizontal-align: middle;" max="20" min="1" step="1" type="range" value="<?php echo esc_attr( $titleooi ); ?>" onchange="updatetwopoststoshowtext(this.value, this.id, 'ceupoststoshow' , 'titleooi','ceupoststoshowtext')" >
<script type="text/javascript">
function updatetwopoststoshowtext(thistextvalue, thisfieldid, thisoldfromid, thisnewtoidone, thisnewtoidtwo) {
updatepoststoshowtext(thistextvalue, thisfieldid, thisoldfromid , thisnewtoidone);
updatepoststoshowtext(thistextvalue, thisfieldid, thisoldfromid , thisnewtoidtwo);
}
</script>
</td>
<td>
<input style="width: 90%;" type="text" id="<?php echo esc_attr( $this->get_field_id( 'ceupoststoshowtext' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'ceupoststoshowtext') ); ?>" type="text" value="<?php echo esc_attr( $titleooi ); ?>" onchange="updatepoststoshowtext(this.value, this.id, 'ceupoststoshowtext' , 'titleooi')" >
<script type="text/javascript">
function updatepoststoshowtext(thistextvalue, thisfieldid, thisoldfromid, thisnewtoid) {
var strthistextvalue= String(thistextvalue); var strthisfieldid = String(thisfieldid);
var strthisoldfromid=String(thisoldfromid); var strthisnewtoid = String(thisnewtoid);
alert ("UPST hello there! strthistextvalue = " + strthistextvalue + " strthisfieldid = " + strthisfieldid + " strthisoldfromid = " + strthisoldfromid + " strthisnewtoid = " + strthisnewtoid );
var subfieldidlength = strthisfieldid.length - strthisoldfromid.length;
var substrfieldid = strthisfieldid.substring (0, subfieldidlength);
alert ("UPST hello there two! subfieldidlength = " + subfieldidlength + " substrfieldid = " + substrfieldid);
var newstrfieldid = substrfieldid + strthisnewtoid;
alert ("UPST hello there three one! newstrfieldid = " + newstrfieldid);
var newElementId = document.getElementById(newstrfieldid);
alert ("UPST hello there three two! newElementId.value = " + newElementId.value);
(document.getElementById(newstrfieldid)).value = strthistextvalue;
alert ("UPST hello there three! (document.getElementById(newstrfieldid)).value = " + strthistextvalue);
alert ("document.getElementById (newstrfieldid = " + newstrfieldid + " ) . value = thistextvalue = " + thistextvalue);
return;
}
</script>
本文标签: javascripthtml input onchange doesn39t accept anonymous functionStack Overflow
版权声明:本文标题:javascript - html input onchange doesn't accept anonymous function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738552462a2097637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论