View Full Version : Resolved Partial ID Selector
bluewalrus
11-02-2011, 11:34 PM
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.
if ($("#newitem"))
$("#newitem").attr("id", 'edititem' + $itemid);
else
$('a[id^="edititem"]').attr("id", 'edititem' + $author_id);
jscheuer1
11-03-2011, 04:39 AM
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),
if ($("#newitem"))
is always true.
Try it:
<!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:
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:
$('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.
bluewalrus
11-03-2011, 12:07 PM
Yup, it was the conditional problem thanks.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.