Skip to main content
Code Review

Return to Answer

Added info about local context.
Source Link

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

The event call can become more generic by using the event's local context 'this'. From the .bind() documentation:

Within the handler, the keyword this refers to the DOM element to which the handler is bound. To make use of the element in jQuery, it can be passed to the normal $() function.

'this' inside of the click event will be the DOM element that was clicked on that caused the event to fire. Then you would need only the following to handlesetup the click event for all of the elements with the class 'spanval':

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

Then you would need only the following to handle the click event:

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

The event call can become more generic by using the event's local context 'this'. From the .bind() documentation:

Within the handler, the keyword this refers to the DOM element to which the handler is bound. To make use of the element in jQuery, it can be passed to the normal $() function.

'this' inside of the click event will be the DOM element that was clicked on that caused the event to fire. Then you would need only the following to setup the click event for all of the elements with the class 'spanval':

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​
Fixed typo - changed '1' to 'l'
Source Link

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

Then you would need only the following to handle the click event:

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanva1"spanval").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

Then you would need only the following to handle the click event:

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanva1").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

Then you would need only the following to handle the click event:

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​
Source Link

Building off of Danny's answer, if you have access to the HTML, you can add a class to each of your span tags and bind an event to that class:

<span id="spanval1" class="spanval">1</span>
<span id="spanval2" class="spanval">2</span>
<span id="spanval3" class="spanval">3</span>

Then you would need only the following to handle the click event:

$(".spanval").click(function(){
 $('#hourvalue').val($(this).text());
});

And with the rest of the script:

$(document).ready(function() {
 $(".spanva1").click(function(){
 $('#hourvalue').val($(this).text());
 });
 $('.hour_dropdown').hide();
 $("#more").click(function() {
 $('.hour_dropdown').fadeToggle(200);
 });
});​
default

AltStyle によって変換されたページ (->オリジナル) /