2

I have created a modal list using bootstrap and Actually I have a form list created and when entered details, it will land me to a desired page. Register button performs this function. But now I have also added a dependent dropdown(Skills,Level) options. It also has a add button and Whenever I am clicking on add button instead of adding another elements,its landing me to that same page and displays added successfully message. Can anyone help me?

<form id='work' method="post" action="registerdb.php">
<div class="form-group">
<label for="recipient-name" class="control-label">First Name</label>
<div class="input-group"><span class="input-group-addon" id="basic-addon1">           
<span class="glyphicon glyphicon-user"></span></span>
<input type="text"  id = "firstname" name = "firstname" class="form-control"         
 placeholder="First Name" aria-describedby="basic-addon1">
</div>
</div>
<div class="form-group"><label for="exampleInputEmail1">Last Name</label>
<div class="input-group"><span class="input-group-addon" id="basic-addon1">   
<span class="glyphicon glyphicon-user"></span></span>
<input type="text" id = "lastname" name = "lastname" class="form-control"   
placeholder="Last Name" aria-describedby="basic-addon1">
</div>
</div>
<div class="form-group">
<label>Company Name</label>
<div class="input-group"><span class="input-group-addon" id="basic-addon1">      
<span class="glyphicon glyphicon-briefcase"></span></span>
<input type="text" id= "companyname" name = "companyname" class="form-       
control" placeholder="Company Name" aria-describedby="basic-addon1">
</div>
</div>
<div class="form-group"><label for="exampleInputEmail1">E-mail ID</label>
<div class="input-group"><span class="input-group-addon" id="basic-addon1">  
<span class="glyphicon glyphicon-envelope"></span></span>
<input type="text" id = "emailid" name = "emailid" class="form-control"      
placeholder="Email" aria-describedby="basic-addon1">
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<div class="input-group"><span class="input-group-addon" id="basic-addon1">  
<span class="glyphicon glyphicon-asterisk"></span></span>                       
<input type="password" id = "password" name = "password" class="form-  
control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Type of Industry</label>
<div class="input-group">
<select class="input-medium bfh-countries" data-country="US">
<option value="" disabled="disbaled" selected="selected">Please Select a     
Value</option>    
<option value="Accounting">Accounting</option>   
<option value="Aeronautical">Aeronautical</option>
<option value="Agriculture">Agriculture</option>
<option value="Science and Research">Science and Research</option>
</select>
</div>
</div>            
</div>
<hr>
<div align="center">
<button type="button" class="btn btn-primary" data-   
dismiss="modal">Close</button>
<input class="btn btn-primary" type = "submit" name = "submit" value =    
"register" />
 </div>
 </form>
 <script>
 $( "#add" ).click(function() {
 var row = $("#wrapper-1").clone();
 //row.remove("#add");
 $('#wrapper').append(row);
 });
 </script>
 <div id="wrapper" width="100%">
 <div id="wrapper-1" style="float: left; width: 85%;">
 <div id="first" style="float: left; width: 65%;"> 
 <label for="skills">Skills</label>
 <select id="one" class="step1">
 <option value="">Please select an option</option>
 <option>C</option>
 <option>C++</option>
 <option>Java</option>
 <option>PHP</option>
 </select>
 </div>
 <div class="general" style="float: left; width: 35%;"> 
  <label for="skills">Level</label>
  <select id="two" class="step2">
  <option value="">option</option>
  <option>Begineer</option>
  <option>Expert</option>
  <option>Advanced</option>
  </select>
  </div>
  </div>
  <div class="add-general" style="float: left; width: 15%;">
  <button id="add">add</button>
  </div>  
  </div>
  • Can you please give us a jsfiddle? It'll be really helpful as we can fork off of the main fiddle, adjust it and tell you how we got it to work. – weirdpanda Nov 14 '15 at 06:51
  • Hey check this fiddle...http://jsfiddle.net/stark143/qc3tu1f1/1/ –  Nov 14 '15 at 07:12

1 Answers1

0

Your jsfiddle is messed up, you should load external CSS and scripts using the "external" feature on the left. And obviously your local files will not be accessible, so use absolute paths. I took the time to set it up properly this time.

Your HTML also looks messy, but browsers still understand it.

As for your unexpected behaviour, I think this post applies to your situation: Form is submitted when I click on the button in form. How to avoid this?

In essence: you just need to specify a type attribute (with value different from "submit" obviously) so that your "Add" button stops acting as a submit button.

<button id="add" type="button">add</button>

You also have a problem with attaching event listener on your "Add" button: your code must be executed after your button exists in DOM.

2 simple methods:

  • Put your script tag after your button HTML.
  • Have your code executed after DOM is ready / loaded (e.g. use jQuery(document).ready(function() { /** your code **/}) to request jQuery to execute it once DOM is ready).
jQuery(document).ready(function() {
    $( "#add" ).click(function() {
        var row = $("#wrapper-1").clone();
        // You should change the id to avoid multiple elements
        // with same id in your DOM...
        $('#wrapper').append(row);
    });
});

Updated jsfiddle: http://jsfiddle.net/qc3tu1f1/4/

Community
  • 1
  • 1
ghybs
  • 47,565
  • 6
  • 74
  • 99
  • Hey thank you for the answer. I used the code. now add button does not land me to a different page but it neither adds next elements like if i want to add more skills and levels it does not add. But its working in jsfiddle but not in my browser –  Nov 15 '15 at 11:45
  • Yes ofcourse I did change.. Check the entire code here... http://jsfiddle.net/stark143/y4w81210/ –  Nov 15 '15 at 12:34
  • You try using jQuery **before** it is defined in your page (i.e. before the jQuery script). Any JS that needs jQuery must be placed **after** the importing script tag. Updated code: http://jsfiddle.net/y4w81210/1/ I highly suggest you using the Developers Tools, it is very handy for debugging. Hit F12 on most browsers to launch it. – ghybs Nov 15 '15 at 12:44
  • Hey I tried adding the same code in JSfiddle and its working perfectly but not in my browser..What might be the issue? –  Nov 15 '15 at 12:46
  • As I have no clue what "browser" you use nor what you do differently than in the jsfiddle, it is hard for me to help you. – ghybs Nov 15 '15 at 12:50
  • You just nailed it..Its working...Thank you..But when I more elements are added the close and register buttons are getting misplaced. And how can increase the length of modal..any hints would be helpful..Thank you again. –  Nov 16 '15 at 08:26
  • Thank you for the feedback! As for your misplaced buttons, it would probably be a matter of CSS. Feel free to open a new question for it. – ghybs Nov 16 '15 at 08:34