Solved

Could you point a way on how to clear a jQuery variable buffer between  modal callings?

Posted on 2016-10-14
7
96 Views
Last Modified: 2016-10-26
Hi Experts

Could you point a way on how to clear a jQuery variable buffer between  modal callings?

Accordingly to:
The modal view is called - the textbox "Anexo" has no value - so the button "Download anexo" is correctly disabled.
img005
Then modal view is called again -  the textbox "Anexo" has a value but the button "Download anexo" is still disabled.
img006
jQuery:
$('#view').on('show.bs.modal', function () {

	var file = $("#view #anexo").val();
	var parts = file.split('/');
	var file = parts[parts.length-1];
	  
	if(file.length != 0)
	{
		$('#view #download').prop("disabled", false);     
	}
	else
	{
		$('#view #download').prop("disabled", true);
	}
	
	$("#view #anexo").val(file); 
	
});

Open in new window


Any suggestions on workaround this ?

Thanks in advance.
0
Comment
Question by:Eduardo Fuerte
  • 4
  • 3
7 Comments
 

Author Comment

by:Eduardo Fuerte
ID: 41844239
In other words what I mean is:

Could you point a way to clear jQuery,  javascript values dinamically - clear the cache - between modal views callings?
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41844385
There is no cache - if it is not being disabled then something else is going on. Is there any other code on the page that is setting the disabled state

Consider this example
HTML
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#view">
     Open modal
    </button>
    <div class="modal fade" id="view" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
        </div>
        <div class="modal-body">
        <p><input type="text" id="anexo" value="path/download/file.pdf"/> <button id="download" class="btn btn-primary">Download</button></p>
        </div>
        <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

Open in new window

jQuery
$(function() {
  $('#view').on('show.bs.modal', function () {
    var file = $("#anexo").val();
    var parts = file.split('/');
    var file = parts[parts.length-1];
      
    if(file.length != 0)
    {
      $('#view #download').prop("disabled", false);     
    }
    else
    {
      $('#view #download').prop("disabled", true);
    }

    $("#view #anexo").val(file); 

  });
});

Open in new window

Working sample here

The sample shows that the modal is opened showing the modified filename.
If you remove the filename altogether then the button is disabled - put something back in and it is enabled.
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41844399
Although I am guessing this is more in line with what you want. This sample moves the disable code to the change() event on the anexo button.
$(function() {
  $('#view').on('show.bs.modal', function () {
    var file = $("#anexo").val();
    var parts = file.split('/');
    var file = parts[parts.length-1];
      
    $("#anexo").val(file); 

  });
  
  $('#anexo').keyup(function() {
  var state = $(this).val().length == 0;
  $('#download').prop({disabled: state});
  });
});

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41844406
An even better option
$(function() {
  $('#view').on('show.bs.modal', function () {
    var file = $("#anexo").val();
    var parts = file.split('/');
    var file = parts[parts.length-1];
    // Trigger the keyup event so the box is initialised on modal open
    $("#anexo").val(file).trigger('keyup'); 
  });
  
  $('#anexo').on('keyup', function() {
    var state = $(this).val().length == 0;
    $('#download').prop({disabled: state});
  });
});

Open in new window

0
 

Author Comment

by:Eduardo Fuerte
ID: 41846496
Hi @Julian

Unfortunatelly all the 03 options suppressed the textbox  with the file name (even having a file to be downloaded) and habilitates the button in a register that has no file and disabilitates the button when a register has a file.
img001
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41860091
Thanks for help!
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41860145
You are welcome.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question