[jQuery] Re: jQuery Selector Help
On Dec 22, 8:03 am, Dhruva Sagar wrote: > I would suggest you to wrap the sections within * into a div and select that > div. > > Thanks & Regards, > Dhruva Sagar. > [ ... snipped ... ] Unfortunately I don't have control of the generated content. The content is generated by a WordPress Calendar plugin that I am reluctant to modify which is why I would like to find a way to do this with jQuery. Mike
[jQuery] Re: jQuery Selector Help
Well, you selected BR elements, which are empty elements, so it's no mystery why this.innerHTML returns undefined... Also, DIVs shouldn't appear inside SPANs...
[jQuery] Re: jQuery Selector Help
On Dec 22, 12:09 pm, Charlie Griefer wrote: > 2009/12/22 Šime Vidas > > > Well, you selected BR elements, which are empty elements, so it's no > > mystery why this.innerHTML returns undefined... > > > Also, DIVs shouldn't appear inside SPANs... > > He did state that he's using generated HTML. He has no control over it. > > Mike - this isn't really a jQuery problem per se. You're jQuery selectors > match DOM elements. Not so much the contents of those elements. > > What you can do is search for the containing element (in this case, you can > look for a with a class of "event"), and replace all instances of />* with just the . > > $(document).ready(function() { > var newHTML = $('span.event').html().replace(/()\s*\*/g, '$1'); > $('span.event').html(newHTML); > > }); > > The expression is looking for a (or or ) followed by any > white space (including tabs), followed by an asterisk. It replaces that > pattern with the alone (removing the asterisk). > [ ... snipped ... ] Thanks for pointing me in the right direction. This is what I ended up getting to work: jQuery("span.event", ".calendar-table").each(function(){ var html = this.innerHTML.replace(/()\s*\*/ g, \'$1\'); jQuery(this).html(html) ; }) ; I changed the regular expression slightly to eliminate the .* portion and changed .html() to .innerHTML. I don't know enough jQuery to know why I had to do that but had seen it elsewhere so tried it and it worked. Thanks, Mike
[jQuery] Re: jQuery Selector Help
[ ... snipped ... ] > > ... and after looking at your code (which always helps), I see you're > referencing "this" (as opposed to jQuery's $(this)), which is why html() > wouldn't have worked. > > In that case, sure. It's been said that "this" is more efficient than > creating a jQuery reference to it via $(this). > > So to clarify... this.innerHTML is the functional equivalent of > $(this).html(), but without the "cost" of creating a new jQuery object for > each iteration of your each(). > [ ... snipped ... ] Thanks for the explanation. I went back and cleaned up my code to use jQuery(this).html() instead of this.innerHTML and all is well. Mike
Re: [jQuery] Re: jQuery Selector Help
2009/12/22 Šime Vidas > Well, you selected BR elements, which are empty elements, so it's no > mystery why this.innerHTML returns undefined... > > Also, DIVs shouldn't appear inside SPANs... > He did state that he's using generated HTML. He has no control over it. Mike - this isn't really a jQuery problem per se. You're jQuery selectors match DOM elements. Not so much the contents of those elements. What you can do is search for the containing element (in this case, you can look for a with a class of "event"), and replace all instances of * with just the . $(document).ready(function() { var newHTML = $('span.event').html().replace(/()\s*\*/g, '$1'); $('span.event').html(newHTML); }); The expression is looking for a (or or ) followed by any white space (including tabs), followed by an asterisk. It replaces that pattern with the alone (removing the asterisk). Disclaimer: I'm no regex guru, so if anyone sees a way to clean up that expression, please feel free. -- Charlie Griefer http://charlie.griefer.com/ I have failed as much as I have succeeded. But I love my life. I love my wife. And I wish you my kind of success.
Re: [jQuery] Re: jQuery Selector Help
On Tue, Dec 22, 2009 at 10:34 AM, Mike Walsh wrote: > > [ ... snipped ... ] > > Thanks for pointing me in the right direction. This is what I ended > up getting to work: > >jQuery("span.event", ".calendar-table").each(function(){ >var html = this.innerHTML.replace(/()\s*\*/ > g, \'$1\'); >jQuery(this).html(html) ; >}) ; > > I changed the regular expression slightly to eliminate the .* portion > and changed .html() to .innerHTML. I don't know enough jQuery to know > why I had to do that but had seen it elsewhere so tried it and it > worked. > > Thanks, > > Mike > .html() retrieves the innerHTML. Really no functional difference, but for the sake of consistency, since you're leveraging jQuery, I'd prefer to see consistent jQuery code (unless there's a compelling reason not to). And yeah... just a preference. Not saying wrong or right. But I am saying you shouldn't have "-had- to do that" :) Anyway, glad you got it working. That's the important bit :) -- Charlie Griefer http://charlie.griefer.com/ I have failed as much as I have succeeded. But I love my life. I love my wife. And I wish you my kind of success.
Re: [jQuery] Re: jQuery Selector Help
On Tue, Dec 22, 2009 at 10:40 AM, Charlie Griefer wrote: > On Tue, Dec 22, 2009 at 10:34 AM, Mike Walsh wrote: > >> >> [ ... snipped ... ] >> >> Thanks for pointing me in the right direction. This is what I ended >> up getting to work: >> >>jQuery("span.event", ".calendar-table").each(function(){ >>var html = this.innerHTML.replace(/()\s*\*/ >> g, \'$1\'); >>jQuery(this).html(html) ; >>}) ; >> >> I changed the regular expression slightly to eliminate the .* portion >> and changed .html() to .innerHTML. I don't know enough jQuery to know >> why I had to do that but had seen it elsewhere so tried it and it >> worked. >> >> Thanks, >> >> Mike >> > > .html() retrieves the innerHTML. > > Really no functional difference, but for the sake of consistency, since > you're leveraging jQuery, I'd prefer to see consistent jQuery code (unless > there's a compelling reason not to). > > And yeah... just a preference. Not saying wrong or right. But I am saying > you shouldn't have "-had- to do that" :) > > Anyway, glad you got it working. That's the important bit :) ... and after looking at your code (which always helps), I see you're referencing "this" (as opposed to jQuery's $(this)), which is why html() wouldn't have worked. In that case, sure. It's been said that "this" is more efficient than creating a jQuery reference to it via $(this). So to clarify... this.innerHTML is the functional equivalent of $(this).html(), but without the "cost" of creating a new jQuery object for each iteration of your each(). -- Charlie Griefer http://charlie.griefer.com/ I have failed as much as I have succeeded. But I love my life. I love my wife. And I wish you my kind of success.