0

I would like to replace abcName with xyzName?

<tr>
    <td>
        <b class="abcName">Bob</b>
    </td>
</tr>

Using this, but on page load nothing changes:

var bobo = document.getElementsByTagName("td");


function user(a, b, c) {
    try {
        if (bobo[c].innerHTML.match('>' + a)) {
            bobo[c].className = b;
        }
    } catch (e) {}
}


function customs() {
    for (i = 0; i < bobo.length; i++) {

        classChange("Bob", "xyzName", i);

    }
}
setInterval("customs()", 1000);

4 Answers 4

1

Although I'm not real sure if what you're doing with the interval and whatnot is the best approach, you can:

var tidi = document.getElementsByTagName("td");

function changeStyleUser(a, b, c) {
    try {
        if (tidi[c].children[0].innerHTML.indexOf(a) === 0) {
            tidi[c].className = b;
        }
    } catch (e) {}
}


function customizefields() {
    for (i = 0; i < tidi.length; i++) {

        changeStyleUser("Bob", "xyzName", i);

    }
}
setInterval("customizefields()", 1000);

http://jsfiddle.net/zBmjb/

Note, you also had the wrong function name in your for loop as well.

If you use jQuery, this would be MUCH simpler.

EDIT

Using jQuery:

function customizefields(a) {
    $('td b').each(function(){
        if ($(this).text().indexOf(a) === 0) {
            this.className = 'xyzName';
        }
    });
}
setInterval(function(){customizefields('Bob')}, 1000);

http://jsfiddle.net/zBmjb/1/

Also, note the use of the anonymous function instead of using a string in setInterval(). This allows you to not use eval(), which is considered expensive and potentially harmful.

EDIT 2

If you wanted to pass in a list of name and class associations, you could use an array with objects, like so:

function customizefields(arrNames) {
    $('td b').each(function(){
        for (var i = 0; i < arrNames.length; i++) {
            if ($(this).text().indexOf(arrNames[i].name) === 0) {
                this.className = arrNames[i].class;
            }
        }
    });
}
var namesToChange = [
    {'name':'Bob','class':'Bob'},
    {'name':'Bert','class':'Bert'},
    {'name':'Jeff','class':'Jeff'}
];
setInterval(function(){customizefields(namesToChange)}, 1000);

http://jsfiddle.net/zBmjb/4/

It feels messy though, since it searches for all selected nodes, then for each found, loops over the current node for each name looking for a matched value, all while doing this once a second. The cleaner approach would be to see if the name value from the current node was found:

function customizefields(objNames) {
    $('td b').each(function(){
        name = $(this).text();
        if (name.indexOf(" ") != -1) {
            name = name.substring(0, name.indexOf(" "));
        }
        if (objNames[name]) {
            this.className = objNames[name];
        }
    });
}
var namesToChange = {
    'Bob':'Bob',
    'Bert':'Bert',
    'Jeff':'Jeff'
};
setInterval(function(){customizefields(namesToChange)}, 1000);

http://jsfiddle.net/zBmjb/5/

EDIT 3

If you need multiple values, you can make the value for the object an object as well.

function customizefields(objNames) {
    $('td b').each(function(){
        name = $(this).text();
        if (name.indexOf(" ") != -1) {
            name = name.substring(0, name.indexOf(" "));
        }
        if (objNames[name]) {
            this.className = objNames[name].class;
            this.style.backgroundImage = "url(" + objNames[name].img + ")";
        }
    });
}
var namesToChange = {
    'Bob':{'class':'Bob','img':'bobspic.jpg'},
    'Bob':{'class':'Bert','img':'Bertphoto.jpg'},
    'Bob':{'class':'Jeff','img':'jeff.jpg'}
};
setInterval(function(){customizefields(namesToChange)}, 1000);
Sign up to request clarification or add additional context in comments.

7 Comments

@Jared Farrish I wouldn't mind a jquery solution if its a better way to go :)
@Jared Farrish you're a pro!!!! if i wanted to add more names say bob, bert, and jeff and give each name its own class value how would you do that?
@Jared Farrish Your talent amazes me :D If I could bother you with one last request? Could you add this code fixee.org/paste/un7306j into the last example. Its based on the earlier JavaScript I posted. From what I can understand it adds the bgImage class to the td tag based on the name. So if its possible to alter the array to have name:name(class):bgimage(class)
+1 for a solid multiple-answer answer. (and a great avatar photo).
@CleverQuack - Thanks. @sarsar - I just noticed that I had .class instead of .img in the last code edit. FYI.
|
1

Grab the element you want to change (can do this multiple ways, in this example i used ID).

var x = document.getElementById( 'id-of-my-element' );
x.className = 'b';

To remove a class use:

x.className = x.className.replace(/\bCLASSNAME\b/,'');

Hope this helps.

1 Comment

it doesnt have an ID. Only that class for the bold tag.
0

Change this line:

classChange("Bob", "xyzName", i);

to this:

changeStyleUser("Bob", "xyzName", i);

Your script will work fine then. Do yourself a favor and use a debugger. :-)

Comments

0
if (tidi[c].innerHTML.search("class=\"abcName\"")>=0) {
     tidi[c].children[0].className = b;
}

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.