SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question jQuery Get the Adjecent Element type

    Hi

    I have the following DOM structure and I want to know if the Adjecent Element of the selected checkbox is a "text" type element or not..

    Code:
    <ul>
    	<li>
    		<label><input type="checkbox">AA:</label>
    			<ul>
    				<li><label><input type="checkbox" id="bb">BB:</label><input type="text"></li>
    				<li><label><input type="checkbox">CC:</label><input type="radio"></li>
                                   <li><label><input type="checkbox" id="dd">DD:</label><input type="text"></li>
    			</ul>
    	</li>
    </ul>
    I am using the following jQuery code

    Code JavaScript:
     
    $('input[type=checkbox]').bind('click change', function(){
     
      var getElementType = $(this + ' + input').attr('type');
      console.log(getElementType);    
     
     
    });


    but getting this error:
    uncaught exception: Syntax error, unrecognized expression: [object HTMLInputElement]

    In the above DOM structure, it should be for the chekboxes with id "bb" and "dd",


    can someone please help me with this?

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    From the checkbox, you would want to go up to its parent (the label), and then check the next element.

    Code:
    ... = $(this).parent().next().attr('type');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hey paul

    THanks for the solution. It works!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •