简体   繁体   中英

how do i define this object in order to add event listeners to my form? unsure of what to do

i need to define the ShapeLink object in order to allow me to add an event listener to the form that checks for "submit" events to the initForm function. however, when i make one, it says that the ShapeLink object is undefined and i am having trouble defining it

<body>
<form>
    <label for="name">Name:</label>
    <input type="text" id="name">
    <label for="shapeSelect">Shape:</label>
    <select name="" id="shapeSelect">
        <option value="">pick a shape</option>
        <option value="square">Square</option>
        <option value="circle">Circle</option>
    </select>
    <input type="submit" value="Submit">
</form>
<div id="outputText"></div>
<svg width=100 height=100></svg>
<script>
    ShapeLink.initForm(document.querySelector("form"));
</script>
</body>

but i need to add it in my javascript and i am unsure of what goes where and why

let ShapeLink = (function(){
//code here!

return {
    initForm: function(frm){
        //code here!

    }
}
})()

all of my code must be in my javascript. what do i do

I don't know how you are attaching event listener but your code seems to be working:

 let ShapeLink = (function(){ //code here! return { initForm: function(frm){ //code here! frm.addEventListener('submit',function(){ alert('form submitting'); }) } } })() ShapeLink.initForm(document.querySelector("form")); 
 <form> <label for="name">Name:</label> <input type="text" id="name"> <label for="shapeSelect">Shape:</label> <select name="" id="shapeSelect"> <option value="">pick a shape</option> <option value="square">Square</option> <option value="circle">Circle</option> </select> <input type="submit" value="Submit"> </form> <div id="outputText"></div> <svg width=100 height=100></svg> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM