0

This maybe a really simple question but for some reason my code isn't working.

Basically, if a user clicks on a piece of text which has the container class of .valueSD, I want the text to change into a select box so they can update the value of it.

Here is an example of what I did:

$('.valueSD').click(function(){
    $('.valueSD').html( function(){
        var monthDrop = '<td class="valueSD">'+
            '<select name="date-month">'+
                '<option name="01">January</option>'+
                '<option name="02">February</option>'+
                '<option name="03">March</option>'+
                '<option name="04">April</option>'+
                '<option name="05">May</option>'+
                '<option name="06">June</option>'+
                '<option name="07">July</option>'+
                '<option name="08">August</option>'+
                '<option name="09">September</option>'+
                '<option name="10">October</option>'+
                '<option name="11">November</option>'+
                '<option name="12">December</option>'+
            '</select>'+
        '</td>';

        console.log( monthDrop );

        return monthDrop;   
    });
});

For some reason it doesn't work though.

Any ideas why?

4
  • You can pass a string directly to the html() function, like $('#valueSD').html('<td class... etc etc'); Commented Jan 26, 2012 at 15:12
  • jsfiddle.net/tV2Pf/1 There is the JSFiddle :) Commented Jan 26, 2012 at 15:20
  • Sorry that seems to work fine. I have no idea why it doens't work locally. Thanks for your time all! Commented Jan 26, 2012 at 15:22
  • make sure you check for js errors locally. Commented Jan 26, 2012 at 15:39

3 Answers 3

1

Your code seems to work fine.. but the only thing that you need to do is remove the bounded event click else the dropdown will be unselectable just add the code.

$(this).unbind("click"); in the callback function that handles the click event

Demo

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

1 Comment

I think I love you xD I was looking for a while to see why that wasn't working :P Thanks again!
1

You are searching for an ID inside a function , when the TD has a class,

 $('.valueSD').click(function(){
$('.valueSD').html( 
    '<td class="valueSD">'+
        '<select name="date-month">'+
            '<option name="01">January</option>'+
            '<option name="02">February</option>'+
            '<option name="03">March</option>'+
            '<option name="04">April</option>'+
            '<option name="05">May</option>'+
            '<option name="06">June</option>'+
            '<option name="07">July</option>'+
            '<option name="08">August</option>'+
            '<option name="09">September</option>'+
            '<option name="10">October</option>'+
            '<option name="11">November</option>'+
            '<option name="12">December</option>'+
        '</select>'+
    '</td>'  ); });        

Comments

1

You probably want this

$('.valueSD').click(function(){
    $(this).html( function(){
        var monthDrop = '<td class="valueSD">'+
            '<select name="date-month">'+
                '<option name="01">January</option>'+
                '<option name="02">February</option>'+
                '<option name="03">March</option>'+
                '<option name="04">April</option>'+
                '<option name="05">May</option>'+
                '<option name="06">June</option>'+
                '<option name="07">July</option>'+
                '<option name="08">August</option>'+
                '<option name="09">September</option>'+
                '<option name="10">October</option>'+
                '<option name="11">November</option>'+
                '<option name="12">December</option>'+
            '</select>'+
        '</td>';

        console.log( monthDrop );

        return monthDrop;   
    });
});

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.