0
<script type="text/javascript">
function Msg1(){
var a="noida";
id=1;
  document.getElementById('myText').innerHTML = '<p onclick="nice(id)">'+a+'</p>';
}
function Msg2(){
  document.getElementById('myText').innerHTML = 'Try message 1 again...';
}
</script>
<input type="button" onclick="Msg1()" value="Show Message 1" />
<input type="button"  onclick="Msg2()" value="Show Message 2" />
<p id="myText"></p>

when i click on Show Message 1 it send id as a charecter not 1 i want to it send me 1 Thanks

3 Answers 3

4

You could do something like:

'<p onclick="nice(\"' + id + '\")">'+a+'</p>';

Or:

'<p onclick="nice(' + escape(JSON.stringify(id)) + ')">'+a+'</p>';

But this gets very unreadable very quickly.

Using this method you can't send things that aren't easily serializable. A more robust, and involved, solution would use the DOM API and EventListener API.

Example:

var id = { foo: "bar" };
var p = document.createElement("p");
p.addEventListener("click", function () {
    nice(id);
});
p.innerText = "ipsum lorem";
document.body.appendChild(p);
Sign up to request clarification or add additional context in comments.

1 Comment

please do some favor if i want send id in double quote. then its not works
0

You already have concatenation right near where you need it:

'<p onclick="nice('+id+')">'+a+'</p>';

Comments

0

make your innerHTML as

'<p onclick="nice(\"' + id + '\")">'+a+'</p>'

Replace your Msg1() Javascript function as below

function Msg1(){
   var a="noida";
   var id = 1;
   document.getElementById('myText').innerHTML = '<p onclick="nice(\"' + id + '\")">'+a+'</p>';
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.