sam
sam

Reputation: 117

Sending dynamically generated value from javascript function to another javascript function

I'm having another difficulty with my website project. Ok here is my problem...

    <script>
function getTopArtist(){
    if (window.XMLHttpRequest){
        topartist = new XMLHttpRequest();
    }
    else{
        topartist = new ActiveXObject("Microsoft.XMLHTTP");
    }
    topartist.onreadystatechange=function(){
        try{
            if (topartist.readyState==4 && topartist.status==200){
                var ArtistDetails = topartist.responseXML.documentElement.getElementsByTagName("artist");

                for(i=0;i<=2;i++){
                    myartistname = ArtistDetails[i].getElementsByTagName('name')[0].firstChild.nodeValue;
                    alert(myartistname)
                    document.getElementById('topartistdiv').innerHTML+='<a href="javascript:getAlbums(this is the proble here);">Albums</a>';
            }
        }
        catch(er){
            alert("Oops something went wrong!");
        }
    }
    topartist.open("GET","http://localhost/test/topartist.php",true);
    topartist.send(null);
}
</script>

My problem is on line 17 when I'm trying to put the artist name inside the brackets to then I can send them to another function. So let's say it alerts Beyonce I want the link to be like this.

javascript:getAlbums('Beyonce');

I guess it has something to do with special characters but I can't figure it out. Any help will be appreciated.

Upvotes: 0

Views: 266

Answers (2)

Kon
Kon

Reputation: 27441

document.getElementById('topartistdiv').innerHTML += '<a href="javascript:getAlbums(\'Beyonce\');">Albums</a>';

Upvotes: 1

alex
alex

Reputation: 490273

You need to quote the string, and you have already used ' for the JavaScript string and " for the HTML attribute string.

Use escaped single quote, \'.

'<a href="javascript:getAlbums(\'no problemo\');">Albums</a>'

Upvotes: 1

Related Questions