Skip to main content

jquery html manipulation does not work on the select inputs



Please refer to http://jsfiddle.net/7FfMd/6/





run the code, then select "E" from the drop down, another input box appear, however I cannot select option B from the second input.





How can I fix it.





For some reason, I when I select option "D" from the first input again, the second select box does not update too.





Thanks


Comments

  1. That's because you catch the change event on the div that surrounds both dropdowns. When you change the second dropdown, the event handler will be triggered and remove the dropdown where you made the selection and replace it with a new dropdown.

    If you want to bind the change event to the parent element, you need to check in which dropdown the change was made (using event.target), so that you don't recreate all the dropdowns. If you use delegate to bind the event handler, this will contain the element where the change was made:

    jQuery('.change').delegate('select', 'change', function() { ... });


    It might be simpler to bind the event for each select, then you don't have to check wich dropdown that caused the event. If you use delegate, you can bind the event eventhough the element doesn't exist yet, but you need to put a class, name or id on the dropdowns so that they can be identified:

    jQuery('.change').delegate('#firstDropdown', 'change', function() { ... });
    jQuery('.change').delegate('#secondDropdown', 'change', function() { ... });

    ReplyDelete
  2. your jquery was wrong.
    here is my answer
    Markup is as below

    <div class='change'>
    <div id="placeholder2" style="width:600px;">
    <select>
    <option selected="selected">D</option>
    <option >E</option>
    </select>
    </div>
    <div id="placeholder" style="width:600px;"></div>
    </div>


    jQuery should be like this.

    jQuery('#placeholder2>select').change(function(){
    var testVar= $(this).val();
    alert(testVar);
    if (testVar == "D")
    {
    var htmlString = '<select><option>C</option></select>';
    jQuery('#placeholder').html(htmlString);
    } else
    {
    var htmlString = '<select><option>A</option><option>B</option></select>';
    jQuery('#placeholder').html(htmlString);
    }
    });

    ReplyDelete
  3. You were not getting the val of the input, but were getting the val of the div containing the input:

    var testVar= jQuery('#placeholder2').val();


    Should be

    var testVar = jQuery('#placeholder2 select').val();


    Also, the change subscriber should only be on the select, not the div also (to avoid the event firing when the second select is changed):

    jQuery('.change').change(function() {


    Should be

    jQuery('.change select').change(function() {


    See here for updated version.

    ReplyDelete
  4. The problem is that you've bound the change event handler to the <div> element that contains both <select> elements. Whenever you change any of those <select> elements, the change event bubbles up to the <div>. That works fine for the first one, but when you change the second it's firing that code again and replacing the entire <select> element.

    ReplyDelete
  5. That's because you're accessing the selected value with

    var testVar= jQuery('#placeholder2').val();


    This tries to get .val() from the div, not the select. Try this:

    var testVar= jQuery('#placeholder2 select').val();

    ReplyDelete
  6. Don't forget that when you add new elements, your previously applied methods won't have been applied to them.

    You will need to apply the event code to the new elements.

    ReplyDelete
  7. You are creating a div that have the class .change. So the function are handled by all change by its children.

    Have a look at this code:

    http://jsfiddle.net/7FfMd/13/

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.