2

I am trying to addClass to an input if it exists and trigger a click else addClass to different element and trigger a click on it in jQuery but I am having some problems and tried different ways but with no luck. This is the html:

 <td>
     <input type="radio" name="shipping_method" value="free.free" id="free.free" />
     <label for="free.free">Free</label>
     <input type="radio" name="shipping_method" value="flat.flat" id="flat.flat" />
     <label for="flat.flat">Payed</label>
 </td>

and my script :

<script language="Javascript" type="text/javascript">
    $(document).ready(function(){
        if($('#free.free').length > 0) {
            $('#free.free').addClass('sgcheck');
            $('.sgcheck').trigger('click');
            }
        else { 
            $('#flat.flat').addClass('sgcheck');
            $('.sgcheck').trigger('click');
            }
    });
</script>
4
  • Why would you use ID's with periods in them ? Commented Jan 31, 2015 at 15:10
  • Because they are being generated by a foreach in php but I did not post the whole code. Commented Jan 31, 2015 at 15:21
  • Well change your foreach in PHP to generate normal ID's instead. Commented Jan 31, 2015 at 15:22
  • It's related to modifing two more modules and I do not have time to edit them. This is easier :) Commented Jan 31, 2015 at 15:26

1 Answer 1

1

You need to escape dot character in id selector with double \\:

$('#free\\.free').length

otherwise #free.free selector means element with id free and class name free, which your HTML obviously doesn't have.

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

1 Comment

Wow... because of all the coding I did't check this one... I think it's time to stop for today... Thank you very much :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.