x editable value change on dynamically not working

abhi picture abhi · Sep 26, 2014 · Viewed 21.3k times · Source

i have using x editable file for by bootstrap application. now i have a situation where i need to change the value on my span by a button click.value is changing but if i am click on that x editable span,previous value is populated on the text box this is my sample code

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta charset="utf-8" />
    <title>Patient portal</title>
             <meta name="description" content="3 styles with inline editable feature" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
            <link rel="stylesheet" href="assets/css/bootstrap-editable.css" />

</head>

<body ><!-- #section:basics/navbar.layout -->
       <div>
              <span class="editable" id="city"> intial value </span>
              <button  id="change">change value</button>

       </div>
            <script type="text/javascript">
        window.jQuery || document.write("<script src='assets/js/jquery.min.js'>"+"<"+"/script>");
    </script>
    <script src="assets/js/bootstrap.min.js"></script>
            <script src="assets/js/x-editable/bootstrap-editable.min.js"></script>
    <script src="assets/js/x-editable/ace-editable.min.js"></script>

    <!-- inline scripts related to this page -->

    <script type="text/javascript">
  $( document ).ready(function() {
       $.fn.editable.defaults.mode = 'inline';
       $.fn.editableform.loading = "<div class='editableform-loading'><i class='ace-icon fa fa-spinner fa-spin fa-2x light-blue'></i></div>";
   $.fn.editableform.buttons = '<button type="submit" class="btn btn-info editable-submit"><i class="ace-icon fa fa-check"></i></button>'+
                                        '<button type="button" class="btn editable-cancel"><i class="ace-icon fa fa-times"></i></button>'; 
     $('#city').editable({
            type: 'text',
            name: 'FirstName',
            title: 'enter First Name',
            validate: function(value) {
                if($.trim(value) == '') 
                  return ' First Name is required';
                else if($.trim(value).length>30)
                  return 'Only 50 charateres are allowed';
              }

        });



        $('#change').click(function(){
          $('#city').text("changed value")  
        });

    })

   </script>

</body>

here city span is populated with 'intial value' first and while i am click on that span a text box will appear with value 'intial value' ,then if i click on change button span value will be changed to 'changed value'.then if i click on the span again text box is showing the previous value 'intial value' .how can i fix this issue any help will be appreciated

this is jfiddle link jfiddle

Answer

Mysteryos picture Mysteryos · Sep 26, 2014

For setting a new value to an x-editable object, you have to call its method setValue.

Documentation Here

Demo: JSFiddle

Example:

    $('#change').click(function(){
      $('#city').editable('setValue',"changed value");
    });