Solved

Jquery checkbox and tag creation not working in IE8

Posted on 2014-02-25
5
794 Views
Last Modified: 2014-02-25
Hi Experts,

I have got this code working in all browsers inc ie9 - except for ie8.

Its been driving me mad for over 3 hours now so thought I'd get an extra set of eyes on it.

Please Help

Jquery

if ($('#selected-breeds').children(".tag").length === 0) {
        $('#selected-breeds').hide();
    }

    $('#available-breeds :checkbox').click(function () {
        function selectedBreedFade() {
            if (!$('#selected-breeds').find('a').length) {
                $('#selected-breeds').fadeOut('fast');
            } else {
                $('#selected-breeds').fadeIn('fast');
            }

        }


        if ($(this).is(':checked')) {
            var checkedBreed = $(this).parent().next('td').find('label').text();
            $('<a class="tag">&times; <span>'+checkedBreed+'</span>').appendTo('#selected-breeds');
            selectedBreedFade();
        } else {
            var uncheckedBreed = $(this).parent().next('td').find('label').text();
            $('#selected-breeds').find('.tag span:contains(' + uncheckedBreed + ')').parent().fadeOut('fast').remove();
            selectedBreedFade();
        }

        $('#selected-breeds .tag').click(function () {
            var removedBreed = $(this).find('span').text();
            $(this).fadeOut('fast').remove();
            $('#available-breeds').find('label:contains(' + removedBreed + ')').parent().prev('td').find(':checkbox').attr('checked', false);
            selectedBreedFade()
        });

        $('#button-reset-search').click(function () {
            $('#selected-breeds').fadeOut();
            $('#selected-breeds').find('.tag').remove();
            $('#available-breeds :checkbox').prop('checked', false);
        });

    });

Open in new window


Html

<table id="available-breeds">
              <tbody>
                                   <tr><td><input type="checkbox" name="Breed" value="AA" /></td><td><label>Aberdeen Angus (AA)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AAX" /></td><td><label>Aberdeen Angus Cross (AAX)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AR" /></td><td><label>Angler Rotvieh (AR)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="ARX" /></td><td><label>Angler Rotvieh Cross (ARX)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AN" /></td><td><label>Ankole (AN)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AM" /></td><td><label>Armoricaine (AM)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AMX" /></td><td><label>Armoricaine Cross (AMX)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AU" /></td><td><label>Aubrac (AU)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AUX" /></td><td><label>Aubrac Cross (AUX)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="ALL" /></td><td><label>Australian Lowline (ALL)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AY" /></td><td><label>Ayrshire (AY)</label></td></tr>
                   <tr><td><input type="checkbox" name="Breed" value="AYX" /></td><td><label>Ayrshire Cross (AYX)</label></td></tr>
                              
</tbody>
              </table>
           

<div id="selected-breeds" class="form-row">
  <span class="form-title">Selected Breeds</span>
  <!-- Selected Breeds -->
</div>

Open in new window


Many thanks in advance
0
Comment
Question by:mcluff
  • 2
  • 2
5 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39885363
It seems to be choking on the line where you add the <a> to selected-breeds.

Try this instead
        // ... to line 18
        if ($(this).is(':checked')) {
            var checkedBreed = $(this).parent().next('td').find('label').text();
            // USE THIS LINE
            $('#selected-breeds').append($('<a/>').addClass('tag').html('&times; <span>'+checkedBreed+'</span>'));
             // INSTEAD OF THIS ONE
             // $('<a class="tag">&times; <span>'+checkedBreed+'</span>').appendTo('#selected-breeds');
            selectedBreedFade();
        } else {
            var uncheckedBreed = $(this).parent().next('td').find('label').text();
            $('#selected-breeds').find('.tag span:contains(' + uncheckedBreed + ')').parent().fadeOut('fast').remove();
            selectedBreedFade();
        }
        // .....

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39885369
Are you using jquery 2.x?  That will not support ie8.  You have to use 1.1x
0
 

Author Comment

by:mcluff
ID: 39885448
I'm using 1.10

The rest of my jquery works - its just this part.
0
 

Author Closing Comment

by:mcluff
ID: 39885451
Many thanks
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39885464
You are welcome - thanks for the points.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now