MyBB Community Forums

Full Version: Overqualified Selectors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I thought stuff like this:

var expanders = $("div.expcolimage img.expander");

was supposed to be changed to better selectors in jQuery, like this:

var expanders = $(".expcolimage .expander");

Unless it was just missed/planned for later?
Is there any benefit for the change within the current system? I.e: Will this beneficial to theme authors?
If it means we dont have to use an img to effect the expand/collapse then it would be very useful to go with Eric's change.
Yeah it would mean we could use any element as expand/collapse.

This would also have to be changed:

			// Expanding
			if(expandedItem.is(":hidden"))
			{
				expandedItem.toggle("fast");
				element.attr("src", element.attr("src").replace("collapse_collapsed.png", "collapse.png"))
									.attr("alt", "[-]")
									.attr("title", "[-]");
				element.parent().parent('.thead').removeClass('thead_collapsed');
				this.saveCollapsed(controls);
			}
			// Collapsing
			else
			{
				expandedItem.toggle("fast");
				element.attr("src", element.attr("src").replace("collapse.png", "collapse_collapsed.png"))
									.attr("alt", "[+]")
									.attr("title", "[+]");
				element.parent().parent('.thead').addClass('thead_collapsed');
				this.saveCollapsed(controls, 1);
			}

There are probably other instances of this problem laying around, Multiquote used to be one of them.
Hard coded stuff like that is meant to be removed but I don't know about this. From my JS understanding this expects a IMG tag. I will have a look to this, there should be some JS that affects any tag and not just IMG without drastically changing the code.
What I just listed is the easiest way to stop it from targeting an image. Really no elements should be selected (td, img, div, span) but rather just the classes (And more specific ones added if it's too generic). .class loads faster than element.class after all.

http://stackoverflow.com/a/6107638
Thanks Erik I will have a look to this.
I also think it should be changed - expcolimage and expander are classes which aren't used for any other elements, so there is no reason to include div and img.
Here's another one, this makes it hard to use text/divs for on/off/offlock (Example at bottom of OP: http://community.mybb.com/thread-134342.html )

mark_read_imgs = $("img.ajax_mark_read");
Selecting an element based on type is nasty. Changing these as Eric suggests to use the class should definitely be on a list of musts.
Pages: 1 2 3