1

This should be simple - I want to use a javascript variable, 'foo' within the .attr() element as below:

$("#readreplytxt_" + msgid).attr("onClick", "readofferclose(foo)");

I've tried this:

$("#readreplytxt_" + msgid).attr("onClick", "readofferclose("+ foo")");

However no joy.

How can I make this work?

3
  • Why are you trying to do this instead of just calling .click()? Commented Feb 5, 2013 at 14:33
  • "readofferclose(" + foo + ")" But why not just apply .on("click", function() { })? Commented Feb 5, 2013 at 14:33
  • 1
    The console tells you what's wrong with "readofferclose("+ foo")". Commented Feb 5, 2013 at 14:33

3 Answers 3

5

If you are already using a script to assign the click why not use the click() method to bind the event, similar ot this:

$("#readreplytxt_" + msgid).click(function(){
    readofferclose(foo);
});

It will make your javascript nice and unobtrusive too.

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

9 Comments

Basically its a hide/show button, so when the function to show the elements is ran it changes the function ran by the button to the hide function, however each of these functions needs the foo variable passed to them.
+1 This code will create a hidden reference to foo, so the function will see the current value when it will eventually be called.
If you create a new foo variable for each element, then this works.
@Steve_M If you build a hide/show button you maybe will intrested on .toggle() Documentation Toggle
@Steve_M: One approach could be to use 2 classes and use toggleClass(). Or you can use .css("height", "20px"), changing the value 20px for what you need.
|
1

Note you are missing "+" sign.

$("#readreplytxt_" + msgid).attr("onClick", "readofferclose("+ foo + ")");

Comments

1

Attach an event handler instead:

$("#readreplytxt_" + msgid).click(function (event) {
    event.preventDefault(); // prevent the default click action
    readofferclose(foo);
});

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.