tags:

views:

80

answers:

5

I have several places on the page where there is text "Add" This text is in a td tag which is also just above an input with type="hidden" and name="ProductCode" EDIT: I want to remove the "Add' from each td tag.

My guess is that I need something like

$("input:checkbox[name='ProductCode']").this('td').each.text('Add').remove; 

This obviously throws and error and I have no idea if I am even close. Note: I only want to target these and not any other text on page that may have "Add"

<td valign="top">Add<input type="hidden" value="xxxxxxx" name="ProductCode"></td>
<td valign="top">Add<input type="hidden" value="yyyyyyy" name="ProductCode"></td>
<td valign="top">Add<input type="hidden" value="zzzzzzz" name="ProductCode"></td>
+2  A: 

For the correct syntax read the jQuery documentation.

For your specific problem

$('td:has(>:input[name=ProductCode][type=hidden])').html(
    function(index, oldhtml){ return oldhtml.replace('Add',''); }
  );

Update to avoid cases where text starting with Add get messed up..

$('td:has(>:input[name=ProductCode][type=hidden])')
    .each(
        function(){
            var txt = $(this).contents()[0];
            if(txt.nodeType===3 && $(txt).text()=='Add')
                $(txt).remove();
        });
Gaby
And what happens if, for example, the `td` text is "Address"?
LukeH
I agree with LukeH, why are all of these answers so specific to the word 'Add'? The question says the text is to be removed, but does not specify that other words should be left unchanged. I'd assume any text that is there should be removed, as that is the simplest case.
Alex JL
In this case Add is the only text that will every be in that td. There will never be any "address" in that or those td's
@Alex: I disagree. The question itself and the example markup specifically say "Add". That means (to me) that if the text is "Address" or "Additive" then it should not be removed. Neither should those strings be butchered so that they end up as "ress" or "itive".
LukeH
@user357034: Fair enough, and in that case this answer is perfectly fine. It's worth drawing attention to these potential pitfalls though, because future readers might have similar, but more exacting, requirements.
LukeH
@LukeH, good point .. updating answer
Gaby
@LukeH, your 100% correct in your logic. That future reader might be me, lol
@LukeH The question does not say 'if the text isn't add, I want it to remain'. The text just happens to be 'Add' in this case. A general solution would make more sense than going way out of your way to just removing the word 'Add'. It's over interpretation, and is too specific. Removing all of the text would satisfy the specifications, and in the absence of other guidelines is the best solution.
Alex JL
+1  A: 

This code will help you:

$(document).ready(function() {
  $("td:contains(Add) input[name=ProductCode]:hidden").parent().each(function(){
    var el=$(this);  // Get the element

    el.html(el.html().replace(/^Add</g,'<')); // Remove the Add
  }); 
});
Gert G
+1  A: 

This'll work:

$("input[name='ProductCode'][type=hidden]").parent().each (
    function () {
        $(this)[0].innerHTML = $(this)[0].innerHTML.replace (/Add/i, '');
    } 
);
Brock Adams
This one does work but wow all that code just to remove some txt, lol
And what happens if, for example, the `td` text is "Address"?
LukeH
@user357034: Huh? It's the same amount of code (usually less) than the other answers. I just formatted it with more whitespace per my house's style guides.
Brock Adams
I didn't say is wasn't good as it was actually the first one I checked. Being a nub I felt that the other answer was just a tad bit easier to follow. But I do appreciate your effort!!!. Each answer allows me to learn and isn't that what this is all about anyway? Thx again!!!
@user357034: You're welcome. Glad to help.
Brock Adams
A: 
$("td:has(input[name=ProductCode]:hidden)").each(function(){
    $(this).contents().first().remove();
});

See it here.

melhosseiny
Tried and this does not work.
What's wrong with it? It works in the fiddle.
melhosseiny
Why it doesn't work I don't know but perhaps its because there are other td's on the page or my code is nested. I guess if code is stand alone it will work but not in my particular case. But thx!!!!
A: 
$('td:contains(Add) > :input[name=ProductCode]:hidden').each(function() {
    var prev = this.previousSibling;
    if (prev && (prev.nodeType === 3) && prev.nodeValue.match(/^\s*Add\s*$/)) {
        $(prev).remove();
    }
});
LukeH
Tried this as well and but did not work.