jquery - Split() not showing when index param is used -
i have this:
$('li').click(function() { var currentsrc = $('img', this).attr('src').tostring(); var grabalt = $('img', this).attr('alt').split('|'); //alert(grabalt[0]); $('.imgsample img').attr('src', currentsrc); $('.projectdescription').html('<p class="projectdescription">' + grabalt[0].join('</p>')); $('.projecttitle').html(grabalt[1]); }); when remove [0] , [1] can alert either , i'm thinking first value [0], replace project description, trying place each in respective place doesn't work @ all.
snippet of relevant html:
<section class="workarea"> <div class="imgsample"><img src="images/samples/testimage2.png" alt="this first part short description. | second part title."></div> <p class="projectdescription">this replaced when image clicked on.</p> <h3 class="projecttitle">to replaced</h3> </section> </div> <nav> <ul class="print"> <li class="lirounded"><img width="50" height="50" src="images/samples/image1.png" alt="should replace content in paragraph. | replace title copy."></li> <li class="lirounded"><img width="50" height="50" src="images/samples/image2.png" alt="second. | replace title 2."></li> <li class="lirounded"><img width="50" height="50" src="images/samples/image3.png" alt="third. | replace title 3."></li> </ul> </nav> is syntax or usage wrong here?
you there, not quite:
$('li').click(function() { var img = $('img', this).get(0); var src = img.src; // no need .attr var alt = img.alt; // ditto var grabalt = alt.split('|'); $('.imgsample img').attr('src', currentsrc); $('.projectdescription').text(grabalt[0]); $('.projecttitle').text(grabalt[1]); }); mostly, don't need put new <p> inside .projectdescription element, needed change text contents.
as @gdoron alluded to, .join() doesn't appear think - takes array of strings, , joins them given separator. doesn't concatenate grabalt[0] </p>.
Comments
Post a Comment