简体   繁体   中英

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.

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>'
document.getElementById('topartistdiv').innerHTML += '<a href="javascript:getAlbums(\'Beyonce\');">Albums</a>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM