Solved

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

Posted on 2016-10-14
7
112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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 58

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

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 58

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 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