0

On clicking the td class="bgimg", I'm calling another function. How do I add a class to the td which I clicked?

/*This function creates a list of tabs*/
BCL.onSearchResponse = function(jsonData) {
    BCL.jsonData = jsonData;
    var str = "<table id=\"playlistTable\" cellspacing=\"1\"><tbody><tr>";
    var html = "";
    for (var i = 0; i < jsonData["items"].length; i++) {
        var playlist = jsonData["items"][i];
        html = "<td class=\"bgimg\" onclick=\"BCL.onPlaylistSelect(" + i +")\">{{name}}</td>";
        str += BCL.markup(html,playlist);
    }
    str += "</tr></tbody></table>";
    //console.log(str);
    document.getElementById("results").innerHTML = str;
    // load the first playlist
    BCL.onPlaylistSelect(0);
}
1
  • Like you want it to be marked as clicked or something like that? Commented Feb 24, 2012 at 21:00

1 Answer 1

1
function hasClass(element,clss) {
    return element.className.match(new RegExp('(\\s|^)'+clss+'(\\s|$)'));
}

function addClass(element,clss) {
    if (!this.hasClass(element,clss)) element.className += " "+clss;
}

BCL.onPlaylistSelect = function(something, element) {
    element.addClass("myClass");
    //do stuff
};

BCL.onSearchResponse = function(jsonData) {
    BCL.jsonData = jsonData;
    var str = "<table id=\"playlistTable\" cellspacing=\"1\"><tbody><tr>";
    var html = "";
    for (var i = 0; i < jsonData["items"].length; i++) {
        var playlist = jsonData["items"][i];
        html = "<td class=\"bgimg\" onclick=\"BCL.onPlaylistSelect(" + i +", this)\">{{name}}</td>";
        str += BCL.markup(html,playlist);
    }
    str += "</tr></tbody></table>";
    //console.log(str);
    document.getElementById("results").innerHTML = str;
    // load the first playlist
    BCL.onPlaylistSelect(0);
};
Sign up to request clarification or add additional context in comments.

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.