0

Using jQuery 1.9.1 and jQuery Mobile 1.3

I have some cloned form elements that consist of a select list and text input. I need the text input's name and id attribute to change and reflect the value of the item selected in the select list. This works initially, on the first set of select list/text input but not when you change the select list again. Nor does it work on cloned elements.

http://jsfiddle.net/mT4vw/

$(function(){
    $('form').on('change', '.mySelect', function(){
        var value = $(this).val();
        $(this).nextAll('.ui-input-search').find('#search').attr({name:value, id:value});
     });
});

Also tried this:

$(function(){
    $('.mySelect').on('change', function(){
        var value = $(this).val();
        $('.mySelect').each(function(){
            $(this).nextAll('.ui-input-search').find('#search').attr({name:value, id:value});
        });
    });
});

To sum up, I need to have this work every time the select list is toggled, only to the next text input. I need this to work on the initial select list/text input as well as cloned instances. Is there a better method than cloning (code is in the jsfiddle)? I like this method because it disables any previously selected options in the select list. Thank you.

Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • 2
    Unwrap the code `$(function () { }` this is equivalent to `.ready` which shouldn't be used with JQM in addition that it fires once. – Omar Jun 25 '13 at 20:48

1 Answers1

3

Of course, it behaves as expected. You are changing the id on select change and then on the next change trying to retrieve with the old id which never exists anymore. Insetad you could use a className or use data-type=search in the selector.

Like this:

$(this).nextAll('.ui-input-search').find('[data-type=search]').attr({name:value, id:value});

Or: Change your markup to add a class say searchBox:

 <input type="search" name="" id="search" class="inline searchBox" value="" placeholder="Search" data-theme="d" data-inline="true">

and then select it with that class which is not a variable in your case unlike name/id

$(this).nextAll('.ui-input-search').find('.searchBox').attr({name:value, id:value});

Fiddle

PSL
  • 123,204
  • 21
  • 253
  • 243
  • 1
    @Omar Thanks and good catch by you too on the original comment on the question.. :) – PSL Jun 25 '13 at 20:57
  • I unwrapped the code so it is now only between the – Dirty Bird Design Jun 25 '13 at 21:01
  • Man not sure what is going on, on that fiddle I get this error repeatedly "Uncaught ReferenceError: disableSelectedOption is not defined " – Dirty Bird Design Jun 25 '13 at 21:13
  • PSL man, that is awesome. Big thanks to you and @Omar, the only question I have is why did Omar state to unwrap it out of a function (that it wouldn't work on doc ready) and you have it in a $(function(){}); and it works? – Dirty Bird Design Jun 25 '13 at 22:18
  • @DirtyBirdDesign You are welcome. I believe @ omar's comment is correct. For making it work in fiddle and desktop i believe document.ready was needed that's why it did not work in fiddle. incase of mobile paginit will get triggered i assume(i dont have much exp using the jquery mobile though). But see this url http://stackoverflow.com/questions/14468659/jquery-mobile-document-ready-vs-page-events – PSL Jun 25 '13 at 22:28