I have the following javascript I use to fade off list elements but it only works on the first item and next element with the property id doesn’t fade off. I need all elements with the correct id to fade off.
[code]
<--this one doesn't work
[/code]
I don’t know enough about javascript to know how to do this. Any help will be greatly appreciated. Thanks
The first thing to address is the use of the same id attribute on both <li> tags. ID’s need to be unique within the same page. If you want to apply the same selector to more than one element, use class instead.
After fixing the issue mentioned by @chrisofarabia, you just have to extract the ID-number of this with a regular expression to get the related element:
I couldn’t get it to work. I tried changing everything to see if I could get it to work. Most of the time I have to change $ to jQuery for jQuery to work as I have prototype also. Here’s my last try.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.checkbox').change(function() {
var id = jQuery(this).attr('12').match(/\d+jQuery/)[0]
, $element = jQuery('#autoUpdate' + id);
if (this.checked) $element.fadeIn('slow');
else $element.fadeOut('slow');
});});
</script>
The $ at the end of the regular expression means “end of input” and has nothing to do with jQuery, so don’t replace it (sorry for $(this), the dollar sign somehow slipped in). Also, there’s no attribute such as 12 – what you want is the id, as in my above snippet. And don’t give up so quickly! :-)
I’m not sure but after rereading this appears you may have misunderstood exactly what I’m trying to do.
I need the script to hide all ids with the same id. It looks like you may have thought I need to hide all ids with autoUpdate[hatever-here] but I’d like to hide the ids that are exactly the same like autoUpdate12 i.e. all with autoUpdate12.
You’re only listening to changes of elements with the class checkbox1 – I suppose you want to listen to changes of all checkboxes. In your first post that’d be checkbox – just make sure that every checkbox has this class. If every checkbox has a unique class with a number at the end but beginning with “checkbox”, this can be queried like
jQuery('[class^="checkbox"]').etc()
You can read more about attribute selectors here. Note that you don’t need to add a separate event listener for every element like in your previous post – one for all checkboxes is enough. This way you don’t have to generate it with PHP.
No, the snippet I posted above listens to changes of all checkboxes with the class checkbox. However, within the event handler function this always refers to the checkbox that was actually changed, so you can get the ID of that specific element – and then get the corresponding <li> element by extracting the number at the end.
I need to change the id to classes but anyway I need hide both ids with say autoUpdate12 above when I click one checkbox. Right now the way the fiddle works is what is actually happening. But I need both id=“autoUpdate12” to hide at the same time. Actuall they need changed to classes but you know what i mean?