user2860857
user2860857

Reputation: 559

cannot pass string to function

I'm using javascript and I try to pass a string to a function , like so

        //example string
        var f="a";

//add button that sends the value of f to the function
     document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere("+f+");'> ";

    function gothere(a){
    alert(a);
    }

I never see the alert and in console I see a is not defined (refers to the f I guess?)

If i set the f var to be a number then I see the alert.

What am I missing?

Thanks in advance

EDIT

I was thinking maybe something like

var buttonnode= document.createElement('input');

document.getElementById("mydiv").appendChild(buttonnode);
buttonnode.onclick=gothere(f);

Wont work for the same reason?

Upvotes: 4

Views: 18871

Answers (5)

KAUSHAL J. SATHWARA
KAUSHAL J. SATHWARA

Reputation: 994

function parameter string value image dynamically from JSON. Since item.product_image2 is a URL string, you need to put it in quotes when you call changeImage inside parameter.

My Function Onclick inside pass parameter.

items+='<img src='+item.product_image1+' id="saleDetailDivGetImg">';
items+="<img src="+item.product_image2+"  onclick='changeImage(\""+item.product_image2+"\");'>";

My Function

<script type="text/javascript">
function changeImage(img)
 {
    document.getElementById("saleDetailDivGetImg").src=img;
    alert(img);
}
</script>

Upvotes: 1

alex025
alex025

Reputation: 173

  1. You need to use single quotation marks for value arguments (see @Nis or @CDspace answer).
  2. Better way to handling dynamic clicks or other events is event binding. See jQuery event binding for example.

Upvotes: 0

CDspace
CDspace

Reputation: 2689

When your HTML get's rendered, you get onclick='gothere(a);', but the actual a variable doesn't exist in this context, you want to pass the value of f, as a string, so you'll need to use onclick='gothere(\""+f+"\");'. Note the extra quotes inside the parens. This will render to onclick='gothere("a");' thus passing the string.

When using a number, it works, because calling onclick='gothere(5);' is valid, since a variable can't be named 5, and it passes the number.

Upvotes: 5

Nis
Nis

Reputation: 1477

How about fixing your code ? You are missing the quotes around the value denoted by variable F. Hence, when variable F is parsed, the function becomes gothere(a) . while a is not a defined variable (but its a value) and hence the error.

Try this !

document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere(\""+f+"\");'> ";

The modified part is onclick='gothere(\""+f+"\");'> "

This should work for you !

Upvotes: 2

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

Actually, you don't have an a in your code. You are using variable f to denote a. So using this would help you:

var f="a";
// write the remains of the code as they are..

function gothere(f) {
  alert(f);
}

Now when you'll call the function, there will be an alert of a in the browser.

Also, try wrapping the content in "" double qoutes to let the code understand that this is a string not a character.

For onclick use

onclick='gothere(" + f + ")'

And now, its onto you to write the value. Maybe the issue is because you're not writing the value for the f.

Try inpecting the error. I am sure there won't be anything.

Or try using the attribute field and change it using jQuery.

Upvotes: 3

Related Questions