Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing Styling When Dynamically Adding New Items Onclick

I have code that displays a table row with 2 inputs styled using jquerymobile.

I'm using jquery to dynamically add rows when a "plus" button is clicked.

The problem:

When I append a row the static items retain their styling, but the appended row isn't styled.

What am I doing wrong?

Here's the code: http://jsfiddle.net/Trident/o9r4csc9/

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
</head>
<body>
<div data-role="page">
  <div data-role="header">
    <h1>Test</h1>
  </div>
  <!-- /header -->

  <div data-role="content">
    <div class="input_fields_wrap">
      <table width="100%" border="0" cellspacing="5" cellpadding="5">
        <tr>
          <td width="180px">Entry price</td>
          <td>Percent of position</td>
        </tr>
        <tr>
          <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>
          <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>
        </tr>
        <tr>
          <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>
          <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>
        </tr>
      </table>
    </div>
    <div align="left">
      <button class="add_field_button" data-icon="plus" data-inline="true" >Add</button>
    </div>
    <!-- When "Add" is clicked, execute python script --> 

  </div>
  <!-- /content --> 

</div>
<!-- /page --> 

<script language="javascript">
<!-- adds or deletes input fields dynamically -->
$(document).ready(function() {
    var max_fields      = 1000; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID   
    var addline         = '<tr>\
                             <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>\
                            <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>\
                            </tr>';

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append(addline); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>
</body>
</html>
like image 618
Sage Avatar asked Mar 18 '23 21:03

Sage


1 Answers

In JQuery mobile you can manually update dynamic content by the following code:

  $(wrapper).trigger('create');  

Just one line and all done.

Note: You have to add jquery script for this. I have added it + the css. Please update the UI issue by your self. The point is that the style is working.

http://jsfiddle.net/o9r4csc9/9/

Happy coding :)

like image 77
Manish Gupta Avatar answered Mar 26 '23 03:03

Manish Gupta