jquery - finding either A or B class within a loop, then executing similar code -



jquery - finding either A or B class within a loop, then executing similar code -

i'm next tutorial larn jquery in 30 days on @ tuts+. have completed day #7, goes on how appendto, prependto, , such.

the lesson ended "useable scenario" span wrapped around specific text in blog post, , wrapped text taken, , prepended paragraph, , floated side create pretty quote example.

here have far @ jsfiddle

i've taken , modified little, "pull quote left" function.

what question is, how create wrapping loop search either "qo_right" or "qo_left" instead of having 2 duplicate functions.

(function() { var qo_right = $('article').find('span.qo_right').each(function (){ var $this = $(this); $('<blockquote></blockquote>', { class: 'qo_right', text: $this.text() }).prependto( $this.closest('p') ); }); var qo_left = $('article').find('span.qo_left').each(function (){ var $this = $(this); $('<blockquote></blockquote>', { class: 'qo_left', text: $this.text() }).prependto( $this.closest('p') ); }); })();

there's many different ways think of doing this, or writing it, i'm running lot of confusion , errors.

thanks!

first of all, target both of them in 1 selector:

$('span.qo_left, span.qo_right')

then in iteration, set class depending on class current element has:

'class': $(this).hasclass('qo_left') ? 'qo_left' : 'qo_right'

of course of study if know qo_left , qo_right, respectively, only classes on span, re-create class attribute entirely:

'class': $(this).attr('class')

note i've quoted "class" in assignment, give improve browser compatibility, since "class" reserved keyword in javascript.

jquery

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -