Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can some one please help me how do i change Text that's between HTML elements using Javascript "if...else methods?

for example:
if that td tag equals "Hello"
HTML
<td>Hello</td>

then change text to "Hi":
HTML
<td>Hi</td>


Not onclick. just while loading the page
Posted

Here is the pure JS code. No jQuery needed:
XML
<body onload="replace()">
        <h1>New Web Project Page</h1>
        <table>
            <tr>
                <td>Hello</td>
                <td>world</td>
            </tr>
            <tr>
                <td>Hello</td>
                <td>world</td>
            </tr>
            <tr>
                <td>Hello</td>
                <td>world</td>
            </tr>
        <table>

        <script>
            function replace() {
                var matches = document.querySelectorAll("td");
                for(var index in matches) {
                    if (matches[index].textContent === "Hello") {
                        matches[index].textContent = "Hi";
                    }
                }
            }
        </script>
    </body>
 
Share this answer
 
Comments
Member 10749093 4-May-14 2:34am    
Wow. code is working 100%. you're amazing. i chose your answer as "Accept Solution" right away.
Thank You Very Much.
Xiao Ling 4-May-14 2:39am    
Thanks for choosing my answer :)
Member 10749093 4-May-14 2:42am    
you deserve it :) it has been a week searching for solution. i was knew it's some how by javascript using "if" but my experience in javascript is very low
I am not sure why would you change the values on load.

Anyway, you can use jQuery onready function to change the cell values.

Something to start with:

XML
<!DOCTYPE HTML>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>
        $(document).ready(function() {
            $(this).find("td").html("Hi");
        });
    </script>
</head>
<body>

    <table>
        <tr>
            <td>Hello</td>
            <td>Hello</td>
            <td>Hello</td>
        </tr>
    <table>

</body>
</html>
 
Share this answer
 
Comments
Member 10749093 3-May-14 11:47am    
thanks for your answer. but that your code it changes the whole td tags and it's fine. but do you know some how like that to change only tags that i want with same text? for example td tag has ID
use :eq() Selector[^]
JavaScript
$('td:eq("Hello")').html("Hi");
 
Share this answer
 
Comments
Member 10749093 3-May-14 12:32pm    
thanks for your answer. can you please be more clear how do i use it? ready function and how and on head or on body and do i need js file like this or not
http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
DamithSL 3-May-14 12:36pm    
yes, do the same way as Manas Bhardwaj's answer
Member 10749093 3-May-14 12:45pm    
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
$(document).ready(function() {
$('td:eq("Hello")').html("Hi");
});
</script>
</head>
<body>

<td>Hi</td>
</body>
</html>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900