Human Being
Human Being

Reputation: 8387

jQuery - find the id is existing in iframe condition fails

I am generating the iframe and its id dynamically.

Now I have a situation to find if the Id is already existing or not.

My code will be,

function createIframe(intxnId){

    alert("Id is : "+"$('#"+intxnId+"')");

    if($("'#ch"+intxnId+"'").length == 0){
        alert("Creating the new Iframe intxnId is: " +intxnId);
        var iframe = "<iframe id='ch"+intxnId+"' src='" + contexPath + "/HeartChat.html?intxnId="+intxnId+"' class='iframeSize'></iframe>";
        alert("iFrame Details : "+iframe);

        $("#chatMessageArea").find("#messageArea").html(iframe);

    }else{
        alert("ID is already present !");
    }
}

My alert for the dynamic Id is ,

enter image description here

But I got the error as,

enter image description here

Good answers are definitely appreciated !

Upvotes: 2

Views: 148

Answers (1)

MrCode
MrCode

Reputation: 64526

Your jQuery selector is invalid, for some reason it is wrapped in single quotes. Change to:

if($("#ch"+intxnId).length == 0){

Currently your selector evaluates to:

$("'#chXYZ'") // wrong, it should evaluate to $("#chXYZ")

Also, there is no code in your question that could produce the alert shown.

Upvotes: 1

Related Questions