Swapnil
Swapnil

Reputation: 151

Jquery foreach loop to set values

<span name="abc">ashgs</span>
<span name="abc">ashdf</span>
<span name="abc">asdxdx</span>
<span name="abc">mnnkfvdj</span>
<span name="abc">bsbdb</span>

I want to set a particular value in all these span's using jquery foreach loop Please help as i am poor i jquery Thanks in advance

Upvotes: 2

Views: 8220

Answers (4)

Jai
Jai

Reputation: 74738

Don't understood what do you want to achieve, here is a effort which i understood with your question:


$('span[name="abc"]').each(function (i, v) {
  var valArr = ['Now!', 'This', 'is', 'placed', 'better'];
  $(this).text(valArr[i]);
});

find this in fiddle: http://jsfiddle.net/hWExC/


Or a better one:

$('span[name="abc"]').text(function () {
    var valArr = ['Now!', 'This', 'is', 'placed', 'better', 'wow!!!'];
    return valArr[$(this).index()];
});
span{border:solid 1px red; padding:5px; margin:10px 0 0 0; display:block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span name="abc">ashgs</span>
<span name="abc">ashdf</span>
<span name="abc">asdxdx</span>
<span name="abc">mnnkfvdj</span>
<span name="abc">bsbdb</span>

Upvotes: 1

Alex
Alex

Reputation: 7688

This is how you replace all of your .abc classes'value, with another

$('.abc').each(function(){
    $(this).html('hello');
});

or the following way for a value, just as @Sudhir said

$("span[name='abc']")

Working demo: http://jsfiddle.net/hb9LN/

Upvotes: 1

xdazz
xdazz

Reputation: 160833

If you mean set the html content of the span, then

$("span[name='abc']").html('the content');

Not necessary to use .each().

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

do:

$("span[name='abc']").each(function() {
   //set what you want, like rel
   $(this).attr("rel", "some_value");
});

See here; jQuery each()

Upvotes: 1

Related Questions