javascript - pop up form not working Jquery -
i'm trying make form pop . when user clicks on contact me button show pop form trying code know of syntax wrong please !
my html
<div class="col-lg-12"> <article> <h1>first article</h1> lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </article> <form id="form"> <div class="form-group"> <label>email address</label> <input type="email" class="form-control" id="exampleinputemail1" placeholder="email"> </div> <div class="form-group"> <label>password</label> <input type="password" class="form-control" id="exampleinputpassword1" placeholder="password"> </div> <button type="submit" class="btn btn-default">submit</button> </form> </div>
my jquery
(function(){ var object = { formtoshow:$('#form'), showform: function(){ $(this).object.formtoshow.show(); } }; $('<button class="btn btn-default">contact me</button>').insertafter('article:first').on('click', object.showform); })();
the problem here $(this).object
undefined, causes script error in click handler.
this
inside handler refers dom element, , jquery object $(this)
not have property called object
.
one possible solution use custom context callback method this
inside showform
when invoked refer object
instance.
$('<button class="btn btn-default">contact me</button>').insertafter('article:first').on('click', object.showform.bind(object));
another solution access closure variable object
directly like
(function () { var object = { formtoshow: $('#form'), showform: function () { object.formtoshow.show(); } }; $('<button class="btn btn-default">contact me</button>').insertafter('article:first').on('click', object.showform); })();
Comments
Post a Comment