views:

54

answers:

2

I feel like this is a simple question, but I am still relatively new to javascript and jquery.

I am developing a site for a touch interface that uses unordered lists and jquery .click functions to take input data. I have a section to input a m:ss time, with 3 divs, each containing a list of digits for time. I need to get the input for each column and set it as a variable. I originally designed the inputs to change form inputs, because I didn't understand javascript very much. It was easy to change the 3 hidden inputs by using div id's, but I can't figure out how to do it now with javascript variables.

Here is my original jquery code...

$("div#time>div>ul>li").click(function() {
    var id = $(this).parents(".time").attr("name");
    var number = $(this).html();
    $("input#"+id).val(number);   });

The last line sets one of 3 hidden inputs equal to whatever was clicked. I need to make it so separate variables take the inputs, then I can manipulate those variables however I want.

Here's a short snippet of the html, to have an idea of how jquery grabs it.

<div id="time">
  <h1>Time</h1>
  <div name="minute" class="time" id="t_minute">
  M :
     <ul>

The full time html is here: link text

Thanks everyone!

I've been using SO to answer many questions I've had, but I couldn't find something for this, so I figured I would join, since I'm sure I will have more questions along the way.

So I have tried adding the following, and I still can't get it to work right.

    window.myValues[id] = number;
event[i].min = myValues["minute"];
event[i].sec = myValues["second"];
event[i].sin = myValues["single"];
event[i].time = String(event[i].min) + String(event[i].sec) + String(event[i].sin);

I tried it both with and without the quotation marks. I have not used window.* for anything, so I'm not very sure how to handle this.

A: 

you should be able to reference the variable from the window[] object, so something like window[id] should do the trick for referencing the variable.

nathan gonzalez
I haven't used window for anything, so I don't really know what to do with your suggestion, although I think you helped snowlord think of something. I added some code to the original post if you want to look
phoffer
+2  A: 

First thing to mention here, don't be unnecessary specific. In your example

$('#time').find('li').click()

should be enough. If I understand you well, you want to store the some data. You might want to use jQuery's $.data method. Example:

$('#time').find('li').click(function(){
    var $this = $(this);
    var name = $this.closest('.time').attr('name');
    $.data(document.body, name, $this.html());
});

This would store the html of the clicked li in a global Object, which can be accessed like

alert($.data(document.body, 'minute'));
jAndy
Thank you! This worked and got the data exactly how I wanted. Also, I had never seen .find or .closest, but they will help a lot! I know I need to simplify my selectors (there are others like that), but I had planned on doing that after I got everything completely working. Those 2 things will help me a lot. This answer was perfect, thank you very much.
phoffer
@phoffer: upvote jAndy's answer too, to show him your appreciation :)
Peter Jaric