Skip to main content

Posts

Showing posts with the label jquery-selectors

jQuery: How to select "from here until the next H2”?

I'm setting up a very straightforward FAQ page with jQuery. Like so: <h2>What happens when you click on this question?</h2> <p>This answer will appear!</p> This is all inside a very specific div, so I'll be selecting the header with $('#faq h2') . Simple, right? Click on the H2, and use this.next() to make the next paragraph show up. (The caveat with this page is that a non-programmer will be maintaining it, which is why I'm not using classes: there's no guarantee that any new entries would have the right classes in them.) So! The problem: <h2>What happens when you click on the next question?</h2> <p>That is an interesting conundrum.</p> <p>Because the maintainer is kind of long-winded</p> <p>and many answers will span a few paragraphs.</p> So how, without adding in div s and classes and whatnot, can I have my this.next() routine select everything between the question-that-was-

How do you remove a class that matches a pattern from the class attribute, but retain the other classes?

I want to delete the classes that end with 'blue' from the class attribute on all tags sample html <p class="text_blue happy">this is blue text</p> <p class="text_red nothappy">this is red text</p> <img class="img_blue nothappy" /> This will give me all the elements with classes that end with 'blue' $('[class$=blue]'); how do I pop these matched classnames off of the class attribute?

Setting the id of a link based on it"s href

$('.portfolioThumbs ul li a').mouseover( function(){ var buttLink = $(this).attr('href') var buttLinkArray = buttLink.split( '/' ); // Split the URL after each / and Create an array of each var pFN = buttLinkArray[2]; // We want the Portfolio Folder Name var url = window.location.pathname; $('.galleryNav ul li a').removeClass('hovered'); $('.galleryNav ul li a' + '#' + pFN).addClass('hovered'); window.location.pathname = url + '#' + pFN; } ); This code allows me to set an ID on each button based on its href when the user "mouseover's" it. Does anyone know how this can be done automatically when the page loads, so that each button in the list gets and ID based on it's href, without any user interaction. Thanks, Dan