Results 1 to 3 of 3

Thread: Partial ID Selector

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Partial ID Selector

    So I have a page where admins edit items, these items have the name newitem or edititem####. The number after edititem varies by what item is being editted currently this works if they only edit one item, or refresh the page. If a different item is selected after the first item the older name remains. Could anyone please tell me what I've done wrong here or if there is a better way to do this? Thanks.

    Code:
    if ($("#newitem"))
    	$("#newitem").attr("id", 'edititem' + $itemid);
    else 
    	$('a[id^="edititem"]').attr("id", 'edititem' + $author_id);
    Last edited by bluewalrus; 11-03-2011 at 12:07 PM.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,190 Times in 3,154 Posts
    Blog Entries
    12

    Default

    I don't fully understand what you're trying to do/talking about. But since $("#newitem") is an object (albeit an empty one if there's no element with that id),

    Code:
    if ($("#newitem"))
    is always true.

    Try it:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
    	if ($("#newitem")){
    		alert('here');
    	}
    </script>
    </head>
    <body>
    
    </body>
    </html>
    alerts 'here'.

    If your objective is to identify whether or not there's an element with that id, you can do:

    Code:
    if ($("#newitem").size())
    That will only be true if there are one or more elements with an id of 'newitem'.

    BTW, your usage of ^= here:

    Code:
    $('a[id^="edititem"]').attr("id", 'edititem' + $author_id);
    looks right to me. And the whole thing should be good, assuming $author_id is defined and is a string.
    Last edited by jscheuer1; 11-03-2011 at 04:28 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bluewalrus (11-03-2011)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Yup, it was the conditional problem thanks.
    Corrections to my coding/thoughts welcome.

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
  •