Jump to content

Input Form Control: Insert a Value and Display It


Recommended Posts

QUESTION ONE:  Please suggest what might be entered where the three ? marks appear in order to achieve the following goal.

THE GOAL:  See appear the value of mil in the input control text field and have this value become the value of $_POST['item_encllength'] when the form is submitted.

			var min = $('#min').val();
			var sec = $('#sec').val();
			var mil = function(min, sec) {
				var millisec = ((min * 60) + sec) * 1000;
				return millisec;
			}
			$('#item_encllength').mousedown(mil, function() {
				$(this).???;
			});				

			<input id='min' type='number' value='Minutes'>
			<input id='sec' type='number' value='Seconds'>
			<input id='item_encllength' form='rss2_feed' type='text' name='item_encllength'>

I have experimented in a variety of ways, but have not been able to find the code necessary to achieve my goal.  Perhaps my approach to the problem is inappropriate.

Roddy

Link to post
Share on other sites

JSG:  How will this make the value of mil appear in the field of the visible input control field?  Are you suggesting that it will automatically appear by changing the CSS of the control field from hidden to visible?  I don't get it.

Is there not some way to select the text field of the #item_encllength and simply change its value from empty to mil?

Link to post
Share on other sites

You just have to set the value of the field.

You can't store the value in the variables or it will never update, store a reference to the elements instead.

var min = document.getElementById("min");
var sec = document.getElementById("sec");
var encllength = document.getElementById("item_encllength");

encllength.addEventListener("mousedown", function() {
  encllength.value = ((min.value * 60) + sec.value) * 1000
}, false);

If you want it to happen when the form is submitted use the form's submit event instead of the mosuedown event.

var min = document.getElementById("min");
var sec = document.getElementById("sec");
var encllength = document.getElementById("item_encllength");
var form = document.getElementById("rss2_feed");

encllength.addEventListener("mousedown", update, false);
form.addEventListener("submit", update, false);

function update() {
  encllength.value = ((min.value * 60) + sec.value) * 1000
}

 

Link to post
Share on other sites

Ingolme:  If all that I must do is set the value of the input form control, then what is likely causing the following code to fail?

		$(document).ready(function() {
			var min = $('#min').val();
			var sec = $('#sec').val();
			var mil = function(min, sec) {
				var millisec = ((min * 60) + sec) * 1000;
				return millisec;
			}
			$('#item_encllength').mousedown(mil, function() {
				$(this).val(mil);
			});				
		});

Roddy

Link to post
Share on other sites

Further exploration has shown that the calculation is not properly consummated, and that depending on the input type I can obtain either 0 or NaN for the value of mil. 

Any suggestions?

Link to post
Share on other sites

The Scientist:  Casting does not help.  I tried casting to parseFloat( ) as well.

According to jQuery the .val( ) function reads numbers.  According to the Mozilla Developer website the value generated by the <input type='number'> is a numerical float.  It does seem to be here where the code fails, however.

var mil = function(min, sec) {
	var millisec = ((min * 60) + sec) * 1000;
	return millisec;
}

And, I have no idea why.

Edited by iwato
Link to post
Share on other sites

Please delete this post, as it contains no new information.

Edited by iwato
Link to post
Share on other sites

            $('#item_encllength').mousedown(mil, function() {
                $(this).val(mil);
            });    

You're using jQuery (I assume) to assign a mousedown handler, and you're passing that the mil function, and then an event handler.  The jQuery documentation says if you pass 2 arguments then the first one is event data which will be passed to the event handler.  So, you're passing the mil function (not the result of that function, I assume, but the documentation doesn't show any examples) as a piece of data to the event handler.  I assume (a lot of assumptions going on here) that the mil function will then be a method on an event object inside the event handler, although I don't know the name of it.  Maybe it's just "eventData".

In your code above, there's no reason to pass mil as a parameter to mousedown, you can just execute it inside the event handler.  What your code is trying to do is set the value of the field to a function, you're not even running the function, just using the actual function itself as the field value.  You need to run the function if you want the result of it.

Link to post
Share on other sites

I realized this as well, when I performed an alert on the value of mil.  Rather, than a number I received a functional expression.  So, I tried the following, but to no avail:

$('#item_encllength').on('mousedown',mil(),function() {
	$(this).val(mil);
});				

Have I confused PHP's variable function with Javascript, still again?  Woe is me.

Link to post
Share on other sites

You're not passing any parameters to the mil() function, it's going to return NaN or undefined.

Given what you have currently. this is the shortest way you can put it. The code I provided earlier is more efficient, though.

$('#item_encllength').on('mousedown',function() {
	$(this).val(mil($('#min').val(), $('#sec').val()));
});	

 

Link to post
Share on other sites

Yes, the following works:

var mil = function(min, sec) {
	var millisec = ((min * 60) + sec) * 1000;
	return millisec;
}
$('#item_encllength').on('mousedown',function() {
	$(this).val(mil($('#min').val(), $('#sec').val()));
});	

and more simply,

var mil = function(min, sec) {
	var millisec = ((min * 60) + sec) * 1000;
	return millisec;
}
$('#item_encllength').mousedown(function() {
	$(this).val(mil($('#min').val(), $('#sec').val()));
});	

Thank you everyone for your kind support.   Problem resolved.  Hooray, hooray!

Link to post
Share on other sites

Note also that doing something like this:

$('#item_encllength').mousedown(mil, function() {

or this:

$('#item_encllength').mousedown(mil(), function() {

Does not create a variable called "mil" inside the event handler.  The jQuery documentation says it uses that value as event data that gets added onto the event object that gets passed to the event handler, it does not create a new local variable inside that function.  The data that you pass (which should actually be an object with properties) is added to the event object as the data property:

https://api.jquery.com/event.data/

Link to post
Share on other sites

JSG:  I see your point

3 hours ago, justsomeguy said:

Does not create a variable called "mil" inside the event handler.  The jQuery documentation says it uses that value as event data that gets added onto the event object that gets passed to the event handler, it does not create a new local variable inside that function.

..., but I disagree with this:

Quote

 The data that you pass (which should actually be an object with properties) is added to the event object as the data property:

According to the indication for the .on( ) method for which the .mousedown( ) is just a shortcut, the value of eventData can be anything.  Only after it is entered must it be retrieved as a property of the event object.  In effect, it appears to be still available for use within the event handler; simply it must be accessed via the event.data object.

Edited by iwato
Link to post
Share on other sites

According to the indication for the .on( ) method for which the .mousedown( ) is just a shortcut, the value of eventData can be anything.  Only after it is entered must it be retrieved as a property of the event object.  In effect, it appears to be still available for use within the event handler; simply it must be accessed via the event.data object.

All of that is fine, but here's the issue: when you pass a value to a function, there is no way to determine the variable name that originally held that value.  There is no way for Javascript to determine that the value you passed used to have a variable called "mil", where it can either define a variable with that name, or create a property with that name.  So if you want to be able to find the value by name, you should put it in an object with a property named whatever you want.

Link to post
Share on other sites
  • 2 weeks later...

Are you saying then that the object, because it is an object is global in nature, and that, if created outside of the function, the same object can be accessed either in or outside of the function?

Link to post
Share on other sites
Quote

Javascript uses the same scope chain to look for any variable, whether it's an object or not.

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...