1

So I'm having troubles understanding what I'm doing wrong here.

The big picture I'm trying to implement is a page with an iframe that is controlled with buttons that change the source of the iframe when pressed. The buttons would be dynamically created from a data structure that I wouldn't know the size of, which means that I needed to implement it as a loop.

So far I just added a pre-populated object and tried to implement the dynamic creation of the buttons to the HTML page, but I'm unable to create the buttons.

The code I'm trying to run is

<HEAD>
<TITLE>Testing stuff</TITLE>
</HEAD>
<BODY onload="script();">
<FORM>
<H2>Dynamically add button to form.</H2>

<span id="fooBar">&nbsp;</span>

</FORM>
</BODY>
<script>

var URLobj = {
url1 : "https://www.lipsum.com/",
url2 : "https://www.cnet.com/news/",
url3 : "https://stackoverflow.com/"
};


function add(name, URL) {

	//Create an input type dynamically.
	var element = document.createElement("BUTTON");

	//Assign different attributes to the element.
	element.setAttribute("type", "button");
	element.setAttribute("value", URL);
	element.setAttribute("name", name);

 alert(name);
	var foo = document.getElementById("foobar");

	//Append the element in page (in span).
  alert('i can reach here');
	foo.appendChild(element);
  alert('i can not reach here');
}
window.onload = function iterator()
{

for (var key in URLobj) {
    if (URLobj.hasOwnProperty(key)) {
			add(key, URLobj[key])
    }
}
}
</script>

Also, does this seem like any good of an approach for this kind of a problem? (Trying to add buttons dynamically) or will my next step prove to be tricky with my current approach (making the buttons control an iframe in the page)?

2
  • 1
    foobar =/= fooBar Commented Apr 11, 2018 at 10:21
  • 1
    Full example: jsfiddle.net/khrismuc/wfkgws6u Commented Apr 11, 2018 at 10:29

2 Answers 2

1

The main issue here is this line:

var foo = document.getElementById("foobar");

As JS is case sensitive, it should be:

var foo = document.getElementById("fooBar");

Pay close attention to the console when debugging stuff like this. This is the error you should see with your original code:

Uncaught TypeError: Cannot read property 'appendChild' of null

Also, the element type should be input based on your usage, not button. See below.

var URLobj = {
  url1: "https://www.lipsum.com/",
  url2: "https://www.cnet.com/news/",
  url3: "https://stackoverflow.com/"
};


function add(name, URL) {

  //Create an input type dynamically.
  var element = document.createElement("input");

  //Assign different attributes to the element.
  element.setAttribute("type", "button");
  element.setAttribute("value", URL);
  element.setAttribute("name", name);

  var foo = document.getElementById("fooBar");

  //Append the element in page (in span).
  foo.appendChild(element);
}
window.onload = function iterator() {

  for (var key in URLobj) {
    if (URLobj.hasOwnProperty(key)) {
      add(key, URLobj[key])
    }
  }
}
<HEAD>
  <TITLE>Testing stuff</TITLE>
</HEAD>

<BODY onload="script();">
  <FORM>
    <H2>Dynamically add button to form.</H2>

    <span id="fooBar">&nbsp;</span>

  </FORM>
</BODY>

Sign up to request clarification or add additional context in comments.

Comments

0

Please check

var foo = document.getElementById("foobar");

To

var foo = document.getElementById("fooBar");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.