1

Inside javaScript function i code like this...

function myFunction(){
.....

completeTreeView = completeTreeView + 
   "<li><input type='checkbox' id=" + name +
   " /><label for='item-0' onclick='myclicktest(\'' + result.name +
   '\')' >" + Address + "</label>";

.......
}

function myclicktest(name){
            alert(name);
}

How to pass and get the result.name

1
  • 1
    1) Don't append HTML as string, use createElement 2) don't use inline event handlers, use addEventListener instead. 3) the codehighlighting from SO already gives you the answer. Commented Dec 10, 2013 at 10:30

6 Answers 6

1

Does this work for you?

function myFunction() {
  // .....

  completeTreeView = completeTreeView + "<li><input type='checkbox' id=" + parent.node.name + " /><label for='item-0' onclick='myclicktest(\'" + result.name + "\')' >" + nodeNameIpAddress + "</label>";

  // .......
}

function myclicktest(name) {
  alert(name);
}

Just notice the quotes before and after result.name.

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

Comments

1

The approach seems to be correct.

Please check the single quote or double quote issues in javascript.

You can find the javascript issue in console if you are using firebug in firefox.

You can also check the "result.name" value in an alert to confirm that you are getting the value.

Thanks

1 Comment

given +1 for providing clue(quote). which help me to solve the problem
1

Try this

<label><input type='checkbox' onclick='myclicktest(this);'>Checkbox</label>

function myclicktest(cb)
{
  display("Clicked, new value = " + cb.checked);
}

Comments

1
completeTreeView = completeTreeView + '<li><input type="checkbox" id="' + parent.node.name + '" /><label for="item-0" onclick="myclicktest(\'' + result.name + '\')">' + nodeNameIpAddress + '</label>';

Html attributes use " and it is better to use ' for javascript strings

So if you cannot reverse :

completeTreeView = completeTreeView + "<li><input type='checkbox' id=" + parent.node.name + " /><label for='item-0' onclick=\"myclicktest('" + result.name + "')\" >" + nodeNameIpAddress + "</label>";

3 Comments

but in my case i need to use ' instead of "
i made it reverse... use " instead of '
given +1 for providing clue to fix my issue.
0

One option you have is define the parameter when you bind the event.

function bindEvToThing(str, el)
{
    var name = str
    el.onmouseup = function()
    {
        alert(name)
    }
}

Comments

0

i fixed it... i used as below.

completeTreeView = completeTreeView + "<li><input type='checkbox' id="+ name+" /><label for='item-0' onclick='myclicktest(\"" +  Address + "\")' >"+ Address+"</label>";

Thanks to all for providing me clue to fix it.

6 Comments

Did none of the other answer solve your problem so that you have to post a different one?
yes @Christoph.. i got syntax error of all the above answer in my way of approach....
There are three other answers telling you exactly this! There is no need for this absolutely redundant answer. Instead you could give credits to one of those answers by accepting them as correct answer...
None of the answer helped me, but gave me clue to fix the issue. you can check my answer. But i provide +1(upvote) to them for providing me clue @Christoph.
I don't see any difference from your answer to the others other than the different variable names which comes from you changing your question after they wrote their answers.
|

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.