Solved

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

Posted on 2016-10-14
7
91 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 52

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 52

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now