SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast 3dom's Avatar
    Join Date
    Mar 2007
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery: ID/name of element?

    I'm new to jQuery - so question seems to be pretty simple - how to find id/name of the element? Code:

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    	$("input[@class=vote]").click(function() {
    		$(this).attr("disabled","disabled");
    		var a = ID/NAME of $(this) HERE
    		alert(a);
    	});
    });
    </script>
    
    <input type="button" class="vote" id="12pictures" name="plus" value="hit me">
    <input type="button" class="vote" id="45comments" name="plus" value="or me">
    Thanks!

    P.S. Spent 2 hours learning documentation - found nothing about id/name extraction (too sleepy probably).
    Last edited by 3dom; Jan 30, 2008 at 15:55. Reason: forgot to tell I searched alot =)

  2. #2
    SitePoint Enthusiast 3dom's Avatar
    Join Date
    Mar 2007
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pardon me - forgot I already did same thing in another script few weeks ago using attr() function - looks like this:

    <script type="text/javascript">
    $(document).ready(function() {
    $("input[@class=vote]").click(function() {
    $(this).attr("disabled","disabled");
    var a = $(this).attr('id');
    alert(a);
    });
    });
    </script>

  3. #3
    SitePoint Enthusiast 3dom's Avatar
    Join Date
    Mar 2007
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now question is - how to pass content to element which ID I've just calculated?

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    	$("input[@class=votepictures]").click(function() {
    		$(this).attr("disabled","disabled");
    		var a = $(this).attr('id');
    		var results = 'picturesresults' + a; // THIS IS TARGET ID
    
    		$(#results).text('new results'); // This hellish thing doesn't work =(
    
    		alert(results);
    	});
    });
    </script>
    
    <input type="button" class="votepictures" id="12" name="plus" value="vote +">
    
    <span class="picturesresults" id="picturesresults12" >picture 12 results</span>
    
    <input type="button" class="votepictures" id="23" name="minus" value="vote -">
    
    <span class="picturesresults" id="picturesresults23" >picture 23 results</span>

    P.S. Of course I know I can replace $(#results).text('new results'); with usual document.getElementById(results).innerHTML = 'new results'; - but it's not too good...
    Last edited by 3dom; Jan 31, 2008 at 03:18.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,687
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    I suspect that you're wanting to add the octothorpe to the beginning of the results string.

    Code Javascript:
    $('#' + results).text('new results');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast 3dom's Avatar
    Join Date
    Mar 2007
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    I suspect that you're wanting to add the octothorpe to the beginning of the results string.

    Code Javascript:
    $('#' + results).text('new results');
    Works. Brilliant. Many thanks!


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
  •