1
function abc(id, name){
   var button = '<img src="/images/abc.png" onclick="getvalue('+id+','+name+')"/>';
   $('Div1').set('html',button);
}

my this code is not working. it gives an error. The error is: suppose value of name is Gaurav. then it gives error Gaurav is not defined. Please help me and tell me where is error.

5 Answers 5

3

That is because you are generating code that uses the string value without delimiters.

If id is 42 and name is Gaurav, you will generate the code getvalue(42,Gaurav) instead of getvalue(42,'Gaurav').

Put apostrophes around the string in the code:

var button = '<img src="/images/abc.png" onclick="getvalue('+id+',\''+name+'\')"/>';

Note that this only works as long as the string values doesn't contain any characters that need encoding, like apostrophes or auotation marks.

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

Comments

0

You need quotes around name and id.

var button = '<img src="/images/abc.png" onclick="getvalue(\''+id+'\',\''+name+'\')"/>';

Comments

0

You need to enclose the function arguments in quotes:

var button = '<img src="/images/abc.png" onclick="getvalue('+id+',\''+name+'\')"/>';

By omitting the quotes, you are passing the undefined variable Gaurav in as an argument to the function. What you really intend is to pass in the string "Gaurav" rather than a variable.

Quotes around id may also be necessary:

var button = '<img src="/images/abc.png" onclick="getvalue(\''+id+'\',\''+name+'\')"/>';

Comments

0

The error likely occurs on the call to getvalue as you are supplanting the value of name and not passing a reference. In essence when the code is emitted it would read...

<img src="/images/abc.png" onclick="getvalue(something, Gaurav)" />

Which will cause a problem for the javascript engine as it has no idea what Gaurav is.. try quoting the value..

var button = '<img src="/images/abc.png" onclick="getvalue(\''+id+'\',\''+name+'\')"/>'

Which will render something to the effect of..

<img src="/images/abc.png" onclick="getvalue('something', 'Gaurav')" />

Comments

0

You should mention what the intent of the code is, as it's not completely clear. I'm guessing that it takes the id and name, and dynamically creates an image. I'm not sure if you're using jQuery or not, since I see the use of "$".

Setting the click handler in this way, with string concatenation, is not as safe as doing it programatically. You can easily run into code injection issues with your original approach. e.g. if the name has the word "O'Reilly" in it.

If you're using jQuery, you can do something like this:

var button = $('<img src="...">').click(function() { getvalue(id, name); });

You can find more examples here: http://api.jquery.com/click/

2 Comments

Ok, in which case you should probably use addEvent(). See: mootorial.com/wiki/mootorial/04-element/01-element.event for examples on doing this, as well as mootools.net/docs/core/Element/Element.Event. You will want to avoid trying to construct event handlers with plain string concatenation because it's a vector for injection attacks.
This is a response function of Ajax in mootools. Now its working as other suggest. thanks for replying.

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.