Please check my code. The code adds the list item but it suddenly disappears. Kindly let me know the error in my code.
<button type="submit" class="btn btn-primary" id="needButton">Need</button>
<button type="submit" class="btn btn-primary btn-success" id="haveButton" >Have</button>
</form>
<div class="myNeed">
<ul class="need">
<li>
The needed items are
</li>
</ul>
</div>
This is js code
$(document).ready(function() {
$("#needButton").click(function () {
var needed = $("#bill").val();
$(".need").append("<li>" + needed + "<li>");
});
});
3
Answers
The issue is because the button click triggers the form to be submit which redirects the page.
To fix this, hook your event handler to the
submit
event of theform
element and callpreventDefault()
on the event. Using this methoid over change thebutton
type means that users can still trigger the action by pressing the return key on the keyboard when the input has focus.i think there is a typo you write id=haveButton but in js code you type #needButton try change that to #haveButton
i did a simple code for here i hop it will help you