1

I have a javascript function like this

function doIt(link) {
    alert(link);
}

And i call this function in below given JS code, where i am creating an a tag and appending it to my html page on runtime:

jQuery.each(data, function (i, val) {
    var card = '<a href="Views/NewsDetail.html" onclick=" doIt(' + val.Img + '); "> \
                <div class=" card"> \
                <img src='+ val.Img + ' /> \
                <p>'+ val.Title + '</p> \
                </div> \
                </a>';
    document.getElementById('News').innerHTML += card;
});

Say for example our val.Img = 'abcd' When i click the a tag it calls doIt(abcd), but i want to call doIt('abcd') passing it as string.

Is there any workaround for this.

2
  • Doesn't it redirect to another page Views/NewsDetail.html when you click anchor tag? Commented Sep 25, 2015 at 11:28
  • Thanx everyone for helping me. though i can only accept one answer but giving +1 for helping me. Commented Sep 25, 2015 at 11:35

6 Answers 6

2

You need to escape quotes like

var card ='<a href="Views/NewsDetail.html" onclick="doIt(\'' + val.Img + '\'); "> \
                                                        //^               ^

As per your current implementation the argument abcd is treated as variable thus you must be getting undefined

Sign up to request clarification or add additional context in comments.

2 Comments

lemme try this and get back to you.
Yeah exactly it is giving me undefined.
2

You can do this one:

var card='<a href="Views/NewsDetail.html;" onClick="Test(\'' + val.Img + '\'); "></a>';

Comments

1

The below HTML string already used both quotes till the characters doIt(. So any string type values inside the quotes, must be escaped using any of the quote-character.

 var card = '<a href="Views/NewsDetail.html" onclick=" doIt(' + val.Img + '); ">'

to [added quote escape character \"]

 var card = '<a href="Views/NewsDetail.html" onclick=" doIt(\"' + val.Img + '\"); ">'

3 Comments

I am talking about the <a> tag. The <img> tag is working fine. Thanx for helping though.
Oh! corrected to anchor tag. As you know in JS both ' and " are same. Just use the other-quote to escape instead of \quote-char. Code looks clean and easy to track string concat issues.
Sorry, in this case we have to escape the double quote as we have already used both the quotes. so any string inside onclick= " ........ " has to be escaped.
0

Yet another option:

   var doItAndRedirect=function(val){
           //do stuff with val here
       window.location = "Views/NewsDetail.html"; 
    }

    jQuery.each(data, function (i, val) {
        var card = '<a href="javascript:doItAndRedirect(\''+ val +'\')">'+
                    '<div class=" card">' +
                    '<img src='+ val.Img + ' />' +
                    '<p>'+ val.Title + '</p>' +
                    '</div>'+
                     '</a>';
        document.getElementById('News').innerHTML += card;
    });

Comments

-1

It is already a string it just doesn't display '' on alert but at the background it is string only.

1 Comment

@MurtazaMunshi but you said in your question that function getting called as doIt(abcd);
-1

doIt("' + val.Img + '");

should do it, but if you have, e.g, " inside val.Img you need to quote those: How do I add slashes to a string in Javascript?

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.