Solved

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

Posted on 2016-10-14
7
106 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 57

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 57

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 57

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 57

Expert Comment

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery menu problem in an older browser 6 33
How does this modal work? 3 34
Which is best Image Resizing Web service 11 42
PHP, Curl and XML 13 15
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 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