1

This is my first time using jquery and while this is a fairly simple task I'm stuck already. I've got a input box with the time of day in it. I would like to create a button to grab the time and send it to a variable (setTime) so I can use the time elsewhere in the script. However I'm having trouble the variable to pass, I've added an alert window but all I get is either a blank alert or an "undefined" alert. The first line

Start Time.... works fine its the setTime stuff that's broken.

Page header: setTime = $('#setTime').text(); $('#formTime').timeEntry({show24Hours: true});

Page body:

<p>Start Time <input type="text" size="2" id="formTime" class="spinners" value="" /> </p>

    <input type="button" value="Set Time" onclick="$('#setTime').val('#formTime');" />
    <input type="button" value="Show Date" onclick="alert(setTime);" /> 

Thanks

1
  • you are more likely to get good answers if you show you've done your research first. "Fix this for me" questions with little research to show are frowned upon. Commented Aug 1, 2012 at 22:39

2 Answers 2

1

You have to make a few changes to your code.
Update your Html by adding some ids for example.

<p>
    Start Time <input type="text" size="2" id="formTime" class="spinners" value="" />
</p>

<input id="setTime" type="button" value="Set Time" />
<input id="showTime" type="button" value="Show Date" /> ​

Personally I don't like assigning script to events within the html controls as they become hard to maintain and add clutter to the page.
You can write script at the bottom of the html page within a script tag or better yet, use an external js file. External js files will also keep your Html clean and your scripts unobtrusive.

var setTime = 0;
var $fromTime = $("#formTime")

$("#setTime").off("click").on("click", function(){
    setTime = $fromTime.val();
});

$("#showTime").off("click").on("click", function(){
    alert(setTime);
});

See working DEMO

Using jQuery can be confusing at times but the on-line documentation is fantastic.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the awe and some-ness in the post i.e. demo and opinion. < which I agree >
@Tats_innit: Thank you. It's appreciated.
0

#setTime means "The element with the id 'setTime'" - you have no element with that id, and the control you are trying to get the value of has no id at all.

timeEntry is not a jQuery method, so will error when you try to call it. If you are using a plugin that you think should add that method then you should say so.

.val('#formTime') will set the value of a form control to the string #formTime. If you want to get the value, don't pass that method an argument … and do assign the return value of the method call to something.

You should probably work through an introduction to programming and JavaScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.