You are viewing limited content. For full access, please sign in.

Question

Question

Javascript to build a string that includes a date field

asked on January 15, 2021

Hello,

I'm trying to use a Single Line and a Date field to build a string to store in a hidden field, but I can't get the value from the date field and have tried a number of things.

These are the fields:

And this is my javascript so far.  I won't have any problem creating the string once I can get both values.

Does anyone have any ideas?  Thanks for looking!

0 0

Answer

SELECTED ANSWER
replied on January 15, 2021

Hi Sarah Webb,

 

I don't see any error in your code.

Anyway I tried in my own computer and get the result.

 

This is my code :

$(document).ready(function ()
{
  	$('#q1 input').change(function()
    	{
        	myfunction();            
        });
  
  	$('#q2 input').change(function()
    	{
        	myfunction();            
        });
});

function myfunction(){
  		var string1 = $('#q1 input').val();
 		var date = $('#q2 input').val();
  		var myreturn = string1+" "+date;
        alert(myreturn); 
}

In my mind, you don't have any return because your field date is empty.

And remember, your function start when you change (means focus off) your field.

 

1 0
replied on January 15, 2021

Thank you so much!

0 0

Replies

replied on January 15, 2021 Show version history

Hey Sarah,

There is an error in your code. In jQuery, $() is a method that expects a string as a parameter, and returns a list of elements. Instead of a string, you are passing a function definition, which is invalid, and explains why your event listeners do not fire.

Instead of this:

    $(function() {
        $('.Prebale input').change(function() {
            BuildPrevBalStr();
        })
    })

Try this:

    $('.Prebale input').change(function() {
        BuildPrevBalStr();
    })

Which you could shorten down to:

    $('.Prebale input').change(BuildPrevBalStr)

 

1 0
replied on January 15, 2021

Very helpful, thanks!

0 0
replied on January 15, 2021

Thanks you so much!

You are not allowed to follow up in this post.

Sign in to reply to this post.