skip to Main Content

i need to add id to div.

my code not work.

    <div class="quote-box-col14" for="seo">
<label for="seo" class="quote-service-seo">SEO</label>
<input type="checkbox" name="seo" id="seo" value="Y" class="checkbox-seo" />
</div>

js code

$('#quote-box-col14').change(function(){
    if($(this).is(":checked")) {
        $( "#quote-box-col14" ).attr( "id", "check" );
    } else {
       $( "#quote-box-col14" ).removeAttr("id");
    }
});

js fiddle – http://jsfiddle.net/3syqfnzk/

3

Answers


  1. You are using wrong id. Use on change event of #seo instead of #quote-box-col14.

    And $( ".quote-box-col14" ) instead of $( "#quote-box-col14" ).

    Use like:

    $('#seo').change(function(){
    
            if($(this).is(":checked")) {
    
                $( ".quote-box-col14" ).attr( "id", "check" );
            } else {
               $( ".quote-box-col14" ).removeAttr("id");
            }
        });
    

    Check Fiddle

    Login or Signup to reply.
  2. First of all there is no id="quote-box-col14". quote-box-col14 is a class.
    Also you are not listening on the checkbox. Simply listen on #seo.

        $('#seo').change(function(){
            if($(this).is(":checked")) {
                $('.quote-box-col14').attr("id", "check");
            } else {
               $('.quote-box-col14').removeAttr("id");
            }
        });
    
    Login or Signup to reply.
  3. <div class="quote-box-col14" for="seo">
      <label for="seo" class="quote-service-seo">SEO</label>
      <input type="checkbox" name="seo" id="seo" value="Y" class="checkbox-seo" />
    </div>
    

    Jquery Code For this should be

    $('#seo').change(function(){
    if($(this).is(":checked")) {
      $( ".quote-box-col14" ).attr( "id", "check" );
    } 
    else 
    {
      $( ".quote-box-col14" ).removeAttr("id");
    }
        });
    

    This will work fine for you because onChange should use for input type for example
    <input type="checkbox" name="seo" id="seo" value="Y" class="checkbox-seo" /> not div
    Also class can be access by . not #

     Note: # use for id and .use for class 
    

    Working Example is Demo Here
    You can view this by inspect Element

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search