views:

46

answers:

2

I am trying to run some JQuery script on a particular page but only if a certain page loads.

The Scenario: We have one page that loads (i.e., webpage.aspx) and it loads different content based on the referring click. (i.e., webpage.aspx?contentId=1, webpage.aspx?contentId=2, webpage.aspx?contentId=3, etc). Here is my problem. I need to have a particular part of the page removed if only one specific contentId is pulled. I am trying to do this in JQuery and can't seem to figure it out.

Here's what i have been working with so far. I realize it's not correct but hopefully it gives you a starting point to work with.

Thanks.

CODE:

$(window).load(function() {
   var $deleteNewRow = $("div.col.w140 tbody:first").find("td:first").parent().remove();
   if($('#dealer-info h1').indexOf('Ferrari') != -1) {
      $deleteNewRow
   }
});
+1  A: 

What you store in your $deleteNewRow variable isn't the jQuery method, that method will already execute. You want to do that method in your if statement, something like this (note that you are also missing the .text() in the if statement):

$(window).load(function() {
   if($('#dealer-info h1').text().indexOf('Ferrari') != -1) {
      $("div.col.w140 tbody:first").find("td:first").parent().remove();
   }
});
rosscj2533
Got it working...thank a million!
Ryan Pitts
A: 

You can use jQuery :contains selector to check for the existence. Here is the docs. Then to delete find the element and then use remove.

$("div.col.w140 tbody:first").find("td:first").parent().remove();
Teja Kantamneni
can you help me by writing out the first part of the code (the part that has the "contains" in it)?
Ryan Pitts
if($("#dealer-info").find("h1:contains('Ferrari')")){ $(".col.w140 tbody:first").find("td:first").parent().remove();}else{}The $("#dealer-info").find("h1:contains('Ferrari')") IS selecting the h1 only if it has Ferrari in it...however, the IF statement is not running properly.
Ryan Pitts