0

How to get the html text of CLASS and use another function to change the html text?

For example, there are lot of tags (td) using the same class. But the inner html of that class will be changed after the function called.

HTML

<table>
 <tr><td class="number">1</td></tr>
 <tr><td class="number">2</td></tr>
 <tr><td class="number">3</td></tr>
 <tr><td class="number">4</td></tr>
 <tr><td class="number">5</td></tr>
</table>

jQuery / JS

function ChangeNumber(){
 var cusid_ele = document.getElementsByClassName('number');
 for (var i = 0; i < cusid_ele.length; ++i) {
  var item = cusid_ele[i];
  item.innerHTML = number(item.innerHTML);
 }
}

function number(n){
 if(n == 1)
  return "one";
 else if(n == 2)
  return "two";
 else if(n == 3)
  return "three";
 else if(n == 4)
  return "four";
 else if(n == 5)
  return "five";
}

window.onload = function(){ ChangeNumber() };

I don't know if my codes are incorrect. Yes, it is not working, so I need help. I just refer to this URL: https://stackoverflow.com/a/15560734/2903208

Could anyone try this? And if success, well that's great!

I want the output that will be:

one
two
three
four
five

Is it possible?

Thanks in advance! ^_^

1
  • you just forgot to add variable name near var Commented Oct 26, 2013 at 7:01

5 Answers 5

2
$(document).ready(function(){
    $('.number').each(function({ // added '
        alert($(this).text()); // added (
    });
});

EDIT: Change the HTML to

<table>
 <tr><td class="number" title="one">1</td></tr>
 <tr><td class="number" title="two">2</td></tr>
 <tr><td class="number" title="three">3</td></tr>
 <tr><td class="number" title="four">4</td></tr>
 <tr><td class="number" title="five">5</td></tr>
</table>

and modify the above function to:

$(document).ready(function(){
    $('.number).each(function({
        alert($this).attr('title'));
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I will try it. I am newbie in jQuery. Thanks again.
simply add the above script in your JS file... Dont forget to add jquery refernce on your page
2
var map = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    ........
}

$('table .number').text(function(_,txt) {
    return map[parseInt($.trim(txt), 10)];
});

FIDDLE

Comments

1

change your line below

FROM THIS

    var  = document.getElementsByClassName('number');

TO

    var cusid_ele = document.getElementsByClassName('number');

Comments

0

You can easily do this by a class selector .

Try this

$(document).ready(function(){
    $('.number').each(function({
        var strhtml = $(this).text();
    });
});

Comments

0

You wrap your function to onload event. But the onload event is a standard event in the DOM, which occurs later, when all content (e.g. images) also has been loaded.

You can use .ready event(The ready event occurs after the HTML document has been loaded).

Try this :

HTML-

<table>
 <tr><td class="number">1</td></tr>
 <tr><td class="number">2</td></tr>
 <tr><td class="number">3</td></tr>
 <tr><td class="number">4</td></tr>
 <tr><td class="number">5</td></tr>
</table>

Script code-

$('document').ready(function(){
    ChangeNumber();
});


function ChangeNumber(){
 var cusid_ele = document.getElementsByClassName('number');        
 for (var i = 0; i < cusid_ele.length; ++i) {
  var item = cusid_ele[i];
  item.innerHTML = number(item.innerHTML);
 }
}

function number(n){
 if(n == 1)
  return "one";
 else if(n == 2)
  return "two";
 else if(n == 3)
  return "three";
 else if(n == 4)
  return "four";
 else if(n == 5)
  return "five";
}

Try is 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.