33068

passing a parameter with onclick function is read as a variable when it should be read as a string

Question:

Here is my issue:

I am creating dynamically a button with an onclick function like this:

$("#test).html('<input type="button" value="Close" onclick="remove('+param1+','+param2+');" />');

The parameters are well read but the function is not trigger, and I've got this error message: "bob is not defined" when bob is the string value of the param1.

Apparently it seems that bob is read as a variable when it should be read as a string, but I don't understand why.

Thanks much for your help!

Answer1:

That's because this string right here:

'onclick="remove('+param1+','+param2+');"'

Will look like this in the end:

'onclick="remove(foo, bar);"'

You probably want it to look like this:

'onclick="remove(\'foo\', \'bar\');"'

So change it to this:

'onclick="remove(\''+param1+'\', \''+param2+'\');"'

You could also do this:

$("#test").html('<input type="button" value="Close" />').find('input[type=button]').click(function () { remove(param1, param2); });

Edit: I also noticed that you were missing one " from your $()-call: $("#test) should be $("#test").

Answer2:

I can suggest you this

<script type="text/javascript"> //<![CDATA[ var i = 0; $(function () { $("#lnkAdder").click(function () { // appending new item $("#Container").append( $("<a>").attr({ "href": "javascript:;" }).text("Click me").click(function () { var data = ++i; alert("I'm clicked, I'm number " + data); }) ); }); }); //]]> </script> <a href="javascript:;" id="lnkAdder">Add item</a> <div id="Container"></div>

The key here is the javascript closure. As you can see there a link called lnkAdder. It is responsible to add anew item into the container. On click it appends a new item into the container. While appending you use jQuery API and create a new element, add attributes and add event listener. In the event listener body you copy the value into an internal variable. They use it as appropriate.

Recommend

  • Rails Query to return users belongs to any cities & not belong to any cities
  • Private chat system MYSQL query to display last message of sender/receiver
  • How do can I get XmlRoot to have a collection of objects?
  • How can I mock ui-router's resolve values when testing a state's configuration?
  • How to retrieve information from antrun back to maven?
  • git add error : “fatal : malloc, out of memory”
  • Possible to “watch” both HAML and SASS at the same time?
  • Why cepheus don't send int without quotes to orion?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • one Local Olampyad Questions on Informatic in 2011
  • Meteor helpers not available in Angular template
  • Change Inet root folder for iis 7
  • NHibernate Validation Localization with S#arp Architecture
  • Using jQuery closest() method with class selector
  • How can I send an e-mail from a vbs script
  • Array.prototype.includes - not transformed with babel
  • Align navbar back button on right side
  • Accessing IRQ description array within a module and displaying action names
  • Window Size for Mac application
  • Build own AppleScript numerical error handling
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Sorting a 2D array using the second column C++
  • How to Embed XSL into XML
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?