JavaScript Displaying a name using Javascript

Am new to javascript and doing my student project. I have created a sample page where user enters the name of a place..

He can enter a maximum of 4 places.. I Just like to have a text of "Place A" on the top when user is entering 1st place and when he clicks on "Add Another Place" then "Place B" needs to be displayed to enter text and same like "Place C" and "Place D".

Here is my code

var i = 0;

function isNumericKey(e) {

  if (window.event) {
    var charCode = window.event.keyCode;
  } else if (e) {
    var charCode = e.which;
  } else {
    return true;
  }

  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    return false;
  }

  return true;

}

function add() {

  var a = document.getElementById("ad").value;

  // alert('Please Enter Details');

  if (a != 'null' || a != '') {
    i++;

    if (i == 1) {

      //document.getElementById("input").reset();

      document.getElementById("Place1").style.display = "none";

      document.getElementById("Place2").style.display = "block";

      document.getElementById("Place2").required = true;

      document.getElementById("Place3").style.display = "none";

      document.getElementById("Place4").style.display = "none";


    } else if (i == 2) {

      //document.getElementById("input").reset();

      document.getElementById("Place1").style.display = "none";

      document.getElementById("Place2").style.display = "none";

      document.getElementById("Place3").style.display = "block";

      document.getElementById("Place3").required = true;

      document.getElementById("Place4").style.display = "none";

    } else if (i == 3) {

      //document.getElementById("input").reset();

      document.getElementById("Place1").style.display = "none";

      document.getElementById("Place2").style.display = "none";

      document.getElementById("Place3").style.display = "none";

      document.getElementById("Place4").style.display = "block";

      document.getElementById("Place4").required = true;


      document.getElementById("ad").style.display = "none";

    }
  }
} 
<form action="abc.php" method="post">

  <table width="600" ;>
    <tr>

      <td><font size=4px><label>Place</label></font>
        <br>
        <br>
      </td>

      <td>
        <input type="text" name="Place1" id="Place1" value="" style="display:block; width: 20vw;height:30px;font-size:14pt;" onkeypress="return isNumericKey(event);" required="true" />
        <br>
      </td>

      <td>
        <input type="text" name="Place2" id="Place2" value="" style="display:none; width: 20vw;height:30px;font-size:14pt;" onkeypress="return isNumericKey(event);" />
        <br>
      </td>

      <td>
        <input type="text" name="Place3" id="Place3" value="" style="display:none; width: 20vw;height:30px;font-size:14pt;" onkeypress="return isNumericKey(event);" />
        <br>
      </td>

      <td>
        <input type="text" name="Place4" id="Place4" value="" style="display:none; width: 20vw;height:30px;font-size:14pt;" onkeypress="return isNumericKey(event);" />
        <br>
      </td>

    </tr>

    <td>
      <input type="button" name="Add Another place" id="ad" value="Add Another place" onclick="add();" style="display: block; 
height: 25px; 
width: 175px; 
border-radius: 25px; 
background-color: #008CBA; 
color: #fff; 
border: #008CBA; 
cursor: pointer;" />
Answer:1

There are lots of syntax error in your HTML code, anyways beside that the solution to your problem is quite simple. I have written the following script to acquire the functionality required

  <script type="text/javascript">
     var i = 1;
    function add() {

        if (i == 1)
        {
            document.getElementById("Place1").style.display = "none";
            document.getElementById("Place2").style.display = "block";

            i++;
        }
        else if(i==2)
        {
            document.getElementById("Place2").style.display = "none";
            document.getElementById("Place3").style.display = "block";
            i++;
        }
        else if (i == 3) {
            document.getElementById("Place3").style.display = "none";
            document.getElementById("Place4").style.display = "block";
            document.getElementById("ad").style.display = "none";
        }
    }
 </script> 

And below is my HTML code

<table>  
<tr>
    <td><label>Place</label></td> 
    <td><input type="text" name="Place1" id="Place1" value="" style="display:block; width: 20px;height:30px;font-size:14pt;"  placeholder="place1"/></td>  
    <td><input type="text" name="Place2" id="Place2" value="" style="display:none; width: 20px;height:30px;font-size:14pt;"  placeholder="place2" /></td> 
    <td><input type="text" name="Place3" id="Place3" value="" style="display:none; width: 20px;height:30px;font-size:14pt;"  placeholder="place3" /></td> 
    <td><input type="text" name="Place4" id="Place4" value="" style="display:none; width: 20px;height:30px;font-size:14pt;"  placeholder="place4" /></td>
</tr>  
<tr>
    <td><input type="button" name="Add Another place" id="ad" value="Add Another place" onclick="add();" /></td>
</tr>

Answer:2

Something like that ???

	var MAX_PLACES = 4;
	var currentEditPlace = 0;
	
	function editPlace(index)
	{
		var places = document.getElementById('places');
		var place = places.getElementsByTagName("li")[index];
		place.style.display = 'block';
		
		var span = place.getElementsByTagName("span")[0];
		var input = document.createElement('input');
		input.setAttribute("type", "text");
		input.setAttribute("value", "");
		span.appendChild(input)
		
		/** Return key => show next place */
		input.onkeydown = function(e) {
			
			/** Validation */
			if (this.value != "" && e.keyCode == 13) {
				addNewPlace();
			}
		}
		
		/** Auto focus new input */
		input.focus();
	}

    function addNewPlace() {
		if (currentEditPlace+1 < MAX_PLACES) {
			currentEditPlace++;
			editPlace(currentEditPlace);
		}
    }
	
	/** Start editing place A */
	editPlace(currentEditPlace);
<ul id="places">
	<li style="display:none;">Place A: <span></span></li>
	<li style="display:none;">Place B: <span></span></li>
	<li style="display:none;">Place C: <span></span></li>
	<li style="display:none;">Place D: <span></span></li>
</ul>
<input type="button" value="Add new place" onclick="addNewPlace()"/>
Answer:3

I am building a web application using ExtJS. In my web application, the users can upload photos to the server directory. In addition, I also save the file path to a database table along with the user ...

I am building a web application using ExtJS. In my web application, the users can upload photos to the server directory. In addition, I also save the file path to a database table along with the user ...

I'd like to insert millions of records as a batch process from MongoDB to Aerospike. I follow the documentation and found this doc: http://www.aerospike.com/docs/client/nodejs/usage/kvs/write.html but ...

I'd like to insert millions of records as a batch process from MongoDB to Aerospike. I follow the documentation and found this doc: http://www.aerospike.com/docs/client/nodejs/usage/kvs/write.html but ...

My website use $(window).bind('hashchange', function ()) to check if redirect is required. However, my site works fine in firefox not in IE9 $(window).bind('hashchange', function () { ...

My website use $(window).bind('hashchange', function ()) to check if redirect is required. However, my site works fine in firefox not in IE9 $(window).bind('hashchange', function () { ...

I have an input: <input type="text" placeholder="filter by country name" /> And a list of lists: <ul> <li v-for="continent in continents"> {{ continent[0]....

I have an input: <input type="text" placeholder="filter by country name" /> And a list of lists: <ul> <li v-for="continent in continents"> {{ continent[0]....

  1. vuejs input filter
  2. vuejs filter input value
  3. vuejs v-model input filter