3

I have a list kind of like below:

<p class='info' data-info='1'>Info 1 <span class='status'></span></p>
<p class='info' data-info='2'>Info 2 <span class='status'></span></p>
<p class='info' data-info='3'>Info 3 <span class='status'></span></p>......etc

What I need to do is run through all the .info elements and if the data-info number is included in an myArray change the span text. How, in jQuery do I match a value from inside an array ?

5
  • 2
    Let's see what you've tried to do, it sounds like you have a solid grasp of the concept. Commented Apr 3, 2013 at 8:35
  • 1
    at present I am looking at looping through the .info elements and using the inArray() method to match the data attribute to the array content however I am not sure if using each() is the best practice. Commented Apr 3, 2013 at 8:38
  • I believe this approach is perfectly fine. If you wanted to use pure javascript you could use a for iteration. But that's what .each() is there for. Commented Apr 3, 2013 at 8:41
  • 1
    stackoverflow.com/a/1883691 Commented Apr 3, 2013 at 8:42
  • so far I've come up with: $('.info').each(function(){var element_to_match=$(this).data('info'); if($.inArray(element_to_match, myArray, 0)){//do the rest here....}); - not tested this as yet and not used $.inArray before. Commented Apr 3, 2013 at 8:47

6 Answers 6

3
$('.info').each(function() {
  $this = $(this);
  if ($.inArray($this.data('info'), myArray) !== -1) {
    $this.children('.status').text('New text value');
  }
});

Here's a fiddle

Sign up to request clarification or add additional context in comments.

Comments

2

Do you need to do something like this:

$(".info").each(function() {
    if($(this).data('info').val() == <INSERT NUMBER TO MATCH>)
    {
       //Do some code here
       //example
       $(this).find("span").text("Some text here")
    }
});

3 Comments

.attr("data-info") can be replaced by .data('info')
$("span").text() will replace the text of all span elements
@billyonecan yes I know, this was just an example. Hence the "//example" above that line.
1

Like the below you can get the data-info value of each 'p' after that you can check this value is present in your array

<p class='info' data-info='1'>Info 1 <span class='status'></span></p>
<p class='info' data-info='2'>Info 2 <span class='status'></span></p>
<p class='info' data-info='3'>Info 3 <span class='status'></span></p>......etc

var info = []
$('p').each(function(){
    info.push($(this).attr('data-info'));
});
alert(info)

Comments

1

Instead of looping through .info elements, how about looping through the myArray.

for (var i = 0; i < myArray.length; i++) {
    $("p[data-info=" + myArray[i] + "] .status").html("Your text");
}

Comments

1
var valueToFind = ['3', '0', '1'];
$('.info').each(function(){
    var infoData = $(this).data('info');
    for(i=0;i<valueToFind.length;i++){
        if(valueToFind[i]==infoData){
            $(this).find('.status').html('found');
        }
    }
});

will do the magic.

here is a live fiddle example http://jsfiddle.net/mayooresan/fw94c/

Comments

0

Here you go

Live example http://jsfiddle.net/mithun/5Ryu5/3/

var infoSpecialArray = [2, 3, 4, 5];
$("p.info").each(function() {
    if($.inArray(parseInt($(this).data('info'), 10), infoSpecialArray) != -1) {
      $(this).find('span.status').html('special')
    }
});

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.