Solved

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

Posted on 2016-10-14
7
83 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 51

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 51

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

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 51

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

746 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

13 Experts available now in Live!

Get 1:1 Help Now