Alin St Alin St - 1 year ago 62
HTML Question

Button doesn't perform action when adding Item to list

I am trying to make an application using the MVC design in javascript where I am getting the input from a textbox and insert it into a listbox.

From my point of view, the MVC should be correctly implemented but there is something wrong when I'm creating an onclick event for the 'Add' button. Nothing happens when it's pressed.

There must be something that I am missing here...Can I get some guidance??

function Model(){

this.addValue = function(songName){
var opt = document.createElement("option");
var name = songName.value;
name = name.toString();

opt.text = name;
opt.value = name;

return clear();

function clear(){
document.getElementById('text').value = '';
document.getElementById('artist').value = '';
document.getElementById('genre').value = '';
document.getElementById('type').value = '';

This is my Controller:

function Controller(view, model){

this.addItem = function(song){

And View:

function View(){

var songName = document.getElementById('text'),
artistName = document.getElementById('artist'),
genre = document.getElementById('genre'),
type = document.getElementById('type'),
addBtn = document.getElementById('click'),
listBox = document.getElementById('lstBox');

this.control = function(controller){
addBtn.onclick = controller.addItem(songName);

This is the HTML for textboxes and Listbox:

<script src="View.js"></script>
<script src="Model.js"></script>
<script src="Controller.js"></script>
<!-- Song Input view -->
<h1>Music List</h1>
<fieldset class="settingsView">
<legend><b><i>Insert Song</i></b></legend><br>
<label for="text">Insert song name:</label><br>
<input type="text" id='text'/><br><br>
<label for="artist">Artist:</label><br>
<input type="text" id="artist"/><br><br>
<label for="genre">Genre</label>
<select id="genre">
<option style='display: none'>
<option value="Pop">Pop</option>
<option value="Rock">Rock</option>
<option value="Dubstep">Dubstep</option>
<option value="Hip-Hop">Hip-Hop</option>
<label for="type">Type:</label>
<select id="type">
<option style='display: none'>
<option value="mp3">MP3</option>
<option value="mp4">MP4</option>
<option value="wmv">WMA</option>
<option value="mpg">WAV</option>
<button class="bttn" type="button" id="click">Add</button><br>
width: 270px;
float: left;

width: 200px;
height: 30px;
margin: 0 auto;
display: block;
<label for="listText">Song List:</label><br>
<select class="listBox" size="10" name="listBox" id="lstBox" multiple>
<option selected>Nothing to see
<option>Michael Jackson
<option>Matilda has Worms
<option>Schnitzel Blast
width: 400px;
height: 500px;
position: relative;
</fieldset> -->
var model = new Model();
var view = new View();
var controller = new Controller(view, model);

Answer Source

You're using document.getElementsById() - there is no getElementSById -notice the S.

There is document.getElementById() which will return a single element.

There is also document.getElementsByTagName() and document.getElementsByClassName(). Both of these will return an HTMLCollection - and array-like object of elements.

Looking at your code, it seems all you need to do is change getElementsById to getElementById

As a final note - the browser console is your friend. Your console would 100% have shown you a bunch of error messages saying document.getElementsById() is not a function - when developing with JS ALWAYS refer to your console.

You also have an issue with how you're assigning your onclick.

It should be like this:

addBtn.onclick = function(){ controller.addItem(songName)};

Here is a working fiddle for you - only change really was the above

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download