0

I'm currently stuck and I don't know what to do. The solution may be simple but since I'm new to the JavaScript environment I am kind of lost.

I created a check option that when pressed, it generates a input form to be filled with a "Add new" button to generate another input form.

In my code, when the button "Add new" is pressed, it already creates a dynamic input form with a "Delete" button BUT, I can't seem to make this "delete" ban work when pressed.

All help is welcome. I am using html with JavaScript. Ultimately I'll be saving these form values in a sql database using pup as my middle man.

PS: I am working on WordPress as the web development since the page is already created in it and Yes, I know that I should be using JavaScript and CSS apart from the main html page.

Here's the code I have so far:

    <!DOCTYPE html>
<html>
<style type="text/css">
  label{
    float:left;
    width: 10em;
    text-align: right;
    margin-right: 10px;
  }
</style>


<script type="text\javascript">
  function checkform(form) {
    // get all the inputs within the submitted form
    var inputs = form.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        // only validate the inputs that have the required attribute
        if(inputs[i].hasAttribute("required")){
            if(inputs[i].value == ""){
                // found an empty field that is required
                alert("Please fill all required fields");
                return false;
            }
        }
    }
    return true;
}
</script>



<script type="text/javascript">

//This function adds another form input when button "Add" is pressed
  var counter = 1;
var limit = 5;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " Co-PI or Co-Investigators");
     }
     else {
          var divName = divName;
          var newDivName = (divName + counter);
          var newdiv = document.createElement('div');
          newdiv.id = "dynamicInput" + counter;
          newdiv.innerHTML = (counter + 1) + ". Co-PI or Co-Investigator Information: <br>" +
          "<label>First Name  : </label><input type='text' name='fname[]' maxlength='35' required><br> " +
           "<label>Last Name  : </label><input type='text' name='lname[]' maxlength='35'><br> " +
           "<label>Degree  : </label><input type='text' name='degree[]' maxlength='10'><br> " +
           "<label>Current Position  : </label><input type='text' name='cPos[]' maxlength='30'><br> " +
           "<label>Institution  : </label><input type='text' name='inst[]' maxlength='40'><br> " +
           "<label>School  : </label><input type='text' name='school[]' maxlength='40'><br> " +
           "<label>Department  : </label><input type='text' name='depart[]' maxlength='40'><br> " +
           "<label>Program  : </label><input type='text' name='program[]' maxlength='40'><br> " +
           "<label>Email  : </label><input type='text' name='email[]' maxlength='255'><br> " +
           "<label>Phone (xxx-xxx-xxxx)  : </label><input type='text' name='phone[]' maxlength='10'> <br><br>"  + 
           "<input type='button' value='Delete Previous Form' onClick='removeForm(divName, newDivName);'>  ";

          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}

//Delete div form when delete button is pressed
function removeForm(parentDiv, childDiv){
    if(childDiv == parentDiv)
    {
        alert("Cannot delete anymore form entries!");
    }

    else if (document.getElementById(childDiv))
    {
        var child = document.getElementById(childDiv);
        var parent = document.getElementById(parentDiv)
        parent.removeChild(child);
        counter--;
    }

    else
    {
        alert("Form has been removed or does not exist.");
        return false;
    }
}
</script>


<script type="text/javascript">
function aff_div(ladiv) {
  document.getElementById(ladiv).style.display = "inline";
}

function cach_div(ladiv) {
  document.getElementById(ladiv).style.display = "none";
}

function affich_conj() {
  if (document.forms.CoPI_CoInv_Info.yesNoChkbox[0].checked == true) {
    aff_div("dynamicInput");
    aff_divBtn("btn_New_CoPI_Inv");
  }

  if (document.forms.CoPI_CoInv_Info.yesNoChkbox[1].checked == true) {
    cach_div("dynamicInput");
    cach_divBtn("btn_New_CoPI_Inv");
  }
}


function aff_divBtn(BtnDiv) {
  document.getElementById(BtnDiv).style.display = "inline";
}

function cach_divBtn(BtnDiv) {
  document.getElementById(BtnDiv).style.display = "none";
}

</script>


<form id="CoPI_CoInv_Info" onsubmit="return checkform(this)">

<label1>Do you want to add a Co-PI or Co-Investigator? </label1>
  <input type="radio" name="yesNoChkbox" value="yes" onclick="affich_conj();"> yes
  <input type="radio" name="yesNoChkbox" value="no" checked="checked" onclick="affich_conj();"> no
<br><br>

  <div id="dynamicInput">
                 1. Co-PI or Co-Investigator Information: <br>
       <label>First Name  : </label><input type="text" name="fname[]" maxlength="35" required><br> 
       <label>Last Name  : </label><input type="text" name="lname[]" maxlength="35"><br> 
       <label>Degree  : </label><input type="text" name="degree[]" maxlength="10"><br>
       <label>Current Position  : </label><input type="text" name="cPos[]" maxlength="30"><br>
       <label>Institution  : </label><input type="text" name="inst[]" maxlength="40"><br>
       <label>School  : </label><input type="text" name="school[]" maxlength="40"><br>
       <label>Department  : </label><input type="text" name="depart[]" maxlength="40"><br>
       <label>Program  : </label><input type="text" name="program[]" maxlength="40"><br>
       <label>Email  : </label><input type="text" name="email[]" maxlength="255"><br>
       <label>Phone (xxx-xxx-xxxx)  : </label><input type="text" name="phone[]" maxlength="10"> <br><br>

     </div>
<divBtn id="btn_New_CoPI_Inv">     
<input type="button" value="Add New Co-PI or Co-Investigator..." onClick="addInput('dynamicInput');">  

</divBtn>    
</form>

<script type="text/javascript">
  cach_div("dynamicInput");
  cach_divBtn("btn_New_CoPI_Inv");
</script>
</html>

2 Answers 2

1

You need to quote and escape the variables within the string ~ look at the inline event handler.

  var divName = divName;
  var newDivName = (divName + counter);
  var newdiv = document.createElement('div');
  newdiv.id = "dynamicInput" + counter;
  newdiv.innerHTML = (counter + 1) + ". Co-PI or Co-Investigator Information: <br>" +
  "<label>First Name  : </label><input type='text' name='fname[]' maxlength='35' required><br> " +
   "<label>Last Name  : </label><input type='text' name='lname[]' maxlength='35'><br> " +
   "<label>Degree  : </label><input type='text' name='degree[]' maxlength='10'><br> " +
   "<label>Current Position  : </label><input type='text' name='cPos[]' maxlength='30'><br> " +
   "<label>Institution  : </label><input type='text' name='inst[]' maxlength='40'><br> " +
   "<label>School  : </label><input type='text' name='school[]' maxlength='40'><br> " +
   "<label>Department  : </label><input type='text' name='depart[]' maxlength='40'><br> " +
   "<label>Program  : </label><input type='text' name='program[]' maxlength='40'><br> " +
   "<label>Email  : </label><input type='text' name='email[]' maxlength='255'><br> " +
   "<label>Phone (xxx-xxx-xxxx)  : </label><input type='text' name='phone[]' maxlength='10'> <br><br>"  + 
   "<input type='button' value='Delete Previous Form' onClick='removeForm(\""+divName+"\", \""+newDivName+"\");'>  ";

The removeForm function could be simplified if you were to use the parentNode selector as it appears the button is a direct child of the div you create so potentially you could do:

function removeForm(e){
    e.target.parentNode.parentNode.removeChild( e.target.parentNode );
}

and call it in the HTML using

    <input type='button' value='Delete Previous Form' onClick='removeForm(event);'>
Sign up to request clarification or add additional context in comments.

1 Comment

Perrrrrfect! The quotation of the variables solved my problem. This helped ALOT! Thank you soo much! Awesome community!
1

Try pass id of Div you want to remove:

newdiv.id = "dynamicInput" + counter;
newdiv.innerHTML = (counter + 1) + "... <input type='button' value='Delete Previous Form' onClick='removeForm(\"dynamicInput\"" + counter");'>";

Then, in removeForm():

function removeForm(id){
  if(document.getElementById(id)) document.getElementById(id).outterHTML ='';
}

1 Comment

Thanks, I tried it but I think I did something wrong since it didn't work for me. Even so one of our fellow teammates here in stackoverflow solved the issue for us. I can't thank you enough. Ty!

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.