1

I'm trying to grab the Names of some Class Elements in a document (there are several of them) and rotate through them.

The HTML code of the site looks like this:

<div class="parent">
   <div class="item">
      <a class="item-name" href="http://somerandomurl.com">Relevant Item</a>

If I wanted to grab the URL I know the solution already:

function searchItem(itemname) {
    listings = $(".item-name");
    for(var i = 0; i < links.length; i++) {
        element = listing[i];
        if(element.href.indexOf(itemname) !== -1) {
            return true; 
          } 
    }
}

In this case however, I don't want to compare the URL against another URL, I need to grab the Title off the "item-name" class as String and compare it to other Strings.

How can I manage to do this? I have tried a few things already, like listings = $(".market-name market-link").text();.

2
  • Can you create jsfiddle? Commented Jun 8, 2015 at 19:12
  • It's a real website and the javascript part would be a userscript. Does jsfiddle make sense in this case? Commented Jun 8, 2015 at 20:01

2 Answers 2

1

First, yo are looping through the wrong variable.

You should loop through listing instead of links

Then compare it to the element HTML:

function searchItem(itemname) {
    listings = $(".item-name");
    for(var i = 0; i < listing.length; i++) {
        element = listing[i];
        if(element.html().indexOf(itemname) !== -1) {
            return true; 
          } 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, that was a leftover cause I changed the original code to make it more explaining. The problem is that listings = $(".item-name"); doesn't return anything reasonable, if I print it via console.log it says: Listing: [object Object] instead of "Relevant Item". That's my problem and the reason I'm searching for help
0
function searchItem(itemname) {

    if($('a.item-name:contains("' + itemname + '")').length) {
        //console.log("true"); 
        return true;
    }
}

jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.