tags:

views:

135

answers:

3

This is a little complicated so I will try and explain as best as I can. In the following code the goal is to find a td with the text "(Backordered)" and replace it with a link with the following URL

/Email_Me_When_Back_In_Stock.asp?ProductCode=H210-10 

The "H210-10" needs to be extracted from the 1st sibling td, in this case

<td class="smalltext colors_text">H210-10</td>

Note: there can be more than one tr > td with "(Backorder)" in it and I need to do this for each and every one that has "(Backorder)" in it. I am struggling on how to tackle this issue. I have no access to the markup.

<table cellspacing="1" cellpadding="3" border="0" bgcolor="#cccccc">
<tr class="colors_backgroundlight">
<td>Item#</td>
<td>Item Name</td>
<td>Our Price</td>
<td>Qty</td>
<td><b>Add</b></td>
</tr>
<tr bgcolor="#ffffff" class="Multi-Child_Background">
<td class="smalltext colors_text">H211-41</td>
<td class="productnamecolorSMALL colors_productname">J. Herbin Small Glass Dip Pen - Amber Frosted</td>
<td class="smalltext colors_text"><b>$15.75</b></td>
<td><input type="textbox" value="1" name="QTY.H211-41" maxlength="8" size="5"></td>
<td><input type="checkbox" value="H211-41" name="ProductCode"></td> 
</tr>
<tr bgcolor="#ffffff" class="Multi-Child_Background">
<td class="smalltext colors_text">H211-09</td>
<td class="productnamecolorSMALL colors_productname">J. Herbin Small Glass Dip Pen - Black Frosted</td>
<td class="smalltext colors_text"><b>$15.75</b></td>
<td><input type="textbox" value="1" name="QTY.H211-09" maxlength="8" size="5"></td>
<td><input type="checkbox" value="H211-09" name="ProductCode"></td> 
</tr>
<tr bgcolor="#ffffff" class="Multi-Child_Background">
<td class="smalltext colors_text">H210-10</td>
<td class="productnamecolorSMALL colors_productname">J. Herbin Small Glass Dip Pen - Blue Striped</td>
<td class="smalltext colors_text"><b>$15.75</b></td>
<td colspan="2"><b><font color="#cc0000"><span class="331">(Backordered)</span>
</font></b></td>
</tr>
<tr bgcolor="#ffffff" class="Multi-Child_Background">
<td class="smalltext colors_text">H210-30</td>
<td class="productnamecolorSMALL colors_productname">J. Herbin Small Glass Dip Pen - Green Striped</td>
<td class="smalltext colors_text"><b>$15.75</b></td>
<td><input type="textbox" value="1" name="QTY.H210-30" maxlength="8" size="5"></td>
<td><input type="checkbox" value="H210-30" name="ProductCode"></td> 
</tr>
<tr bgcolor="#ffffff" class="Multi-Child_Background">
<td class="smalltext colors_text">H210-20</td>
<td class="productnamecolorSMALL colors_productname">J. Herbin Small Glass Dip Pen - Red Striped</td>
<td class="smalltext colors_text"><b>$15.75</b></td>
<td><input type="textbox" value="1" name="QTY.H210-20" maxlength="8" size="5"></td>
<td><input type="checkbox" value="H210-20" name="ProductCode"></td> 
</tr>
<tr bgcolor="#ffffff" class="Multi-Child_Background">
<td class="smalltext colors_text">H211-77</td>
<td class="productnamecolorSMALL colors_productname">J. Herbin Small Glass Dip Pen - Violet Frosted</td>
<td class="smalltext colors_text"><b>$15.75</b></td>
<td><input type="textbox" value="1" name="QTY.H211-77" maxlength="8" size="5"></td>
<td><input type="checkbox" value="H211-77" name="ProductCode"></td> 
</tr>
<tr><td bgcolor="#ffffff" align="right" class="smalltext colors_text Multi-Child_Background" colspan="5">Check the items you wish to purchase, then click
<input type="hidden" value="Y" name="IsMultiChildAddToCart">
<input type="hidden" value="ShoppingCart.asp" name="ReturnTo">
<input border="0" type="image" alt="J. Herbin Small Glass Dip Pen" name="btnaddtocart" src="/v/vspfiles/templates/140/images/buttons/btn_addtocart.gif">
</td></tr></table>
+2  A: 

See if this works:

$('td:contains("(Backordered)")').each(function(){
    $this = $(this).html('');

    $('<a />').appendTo(this).attr('href', '/Email_Me_When_Back_In_Stock.asp?ProductCode=' + $this.siblings(':first-child').text()).text('Backorder');
});
Yi Jiang
Thanks but this is not working in my code but seems to work in jsFiddle????
On further inspection is works but removes all the above code from the page except the replaced link
@user Not entirely sure which part here works/not works. Can you clarify a little? Maybe you can try dropping the `.empty()`
Yi Jiang
That fixed the code removal issue but now I have 2 links one without the ending and one with the "H210-10" ending. Also I still have the original (Backorder) text as well. The link without the ending is appended to first line after the </table>
@user Try `html('')` instead. Also, have you checked that the HTML is correctly nested?
Yi Jiang
+1  A: 

This works for me:​

$("td:contains('(Backordered)')").each(function() {
    var item = $(this).closest('tr').find('td:first')
    var code = $(item).text();
    $(item).html('<a href="/Email_Me_When_Back_In_Stock.asp?ProductCode='+code+'">'+code+'</a>');
});​​​​​​​​​​​​​​​​

and the js fiddle

Damo
Thanks, that is helpful but I don't want to make the original text "H210-10" into an anchor but want to replace the (Backorder) text with the link. I really want a button with link but that much I think I can do. Just having a hard time with replacement of the "(Backorder)" text right now
A: 

It turns out I did have some limited access to the markup whereas before i thought I did not have any access.

I was able to add a class to the "(backorder)" text as such...

<span class="331">(Backordered)</span>

and by doing that I came of with the following Jquery that worked.

<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('.331').each(function(){
$(this).closest('td').replaceWith( '<td colspan="2"><a href="/Email_Me_When_Back_In_Stock.asp?ProductCode=' + $(this).closest('td').siblings(':first-child').text() + '"><img border="0" src="/v/vspfiles/templates/140/images/buttons/btn_emailbackinstock.gif"</a></tr>');
}); 
});
</script>

Thanks all for your help which helped me into figuring this out.