I am constructing a selector dynamically as following
...code before
my_selector = '"[data-my-selector=' + "'" + id + "'" + ']"';
alert( "The jQuery object is : " + $(my_selector) );
....
This code stops executing in the alert.
The selector is constructed properly and it's value is "[data-my-selector='453']" (if i alert(my_selector)). If i access $("[data-my-selector='453']") via the console, it works.
But when i try to access $(my_selector) from the console, i get the error
Error: Syntax error, unrecognized expression: "[data-my-selector='453']"
Do you have an idea what's the error ?
Thank you
The double quotes shouldn't be part of the variable. When you hardcode the string, you need the double quotes to denote that it's a string.
my_selector = '[data-my-selector="' + id + '"]';
Related
I am dynamically generating a aspxbutton and attaching a client-side event to it.
When the following code is executed,
Dim path As String = HttpRuntime.AppDomainAppVirtualPath + "/PDF.aspx"
button.ClientSideEvents.Click = "window.open('" + path + "', '_blank');"
I get the following javascript error
JavaScript runtime error: Expected ')'
And i'm unsure why. Appreciate the help!
It looks like it's expecting a ) and isn't getting it. It's probably because the single and double quotes are getting messed up here:
button.ClientSideEvents.Click = "window.open('" + path + "', '_blank');"
Try escaping the single quotes using \ like so:
button.ClientSideEvents.Click = "window.open(\'" + path + "\', \'_blank\');"
EDIT: the 'more' link is built from here:
document.getElementById("elenco_fiveboard").insertAdjacentHTML('beforeend',
"<li>" +
"Titolo: " + valore_comando + " " +
"(<a href='javascript:getInfo("+ valore_comando +");'>" +
"more" + "</a>)" +
"</li>");
So I have to wrap valore_comando with hyphens but I get error trying to write
"(<a href='javascript:getInfo(""+ valore_comando +"");'>" +
or
"(<a href='javascript:getInfo('"+ valore_comando +"');'>" +
Sorry but I am not so strong with JS syntax and I am starting from some code that is not my own.
I have this simple JS function:
getInfo = function(title){
worker.port.postMessage("maggiori_informazioni:" + title);
}
I run it passing to the variable title a value but I always get an error. If title is 'example' then JS try to run
getInfo = function(example)
and I get the error:
Reference Error: example is not defined.
If title has more than one word: 'first example'
then JS will try to run
getInfo = function(first example)
and I get the error:
Syntax error: missing ) after argument list (looking for parentesys
after 'first').
What am I missing here??
This code is part of my first test with webSockets. If you want to see the full code you can open this index page and put a value on the first alert you see. That value is the title that you will see on the dashboard page.
The error can be reproduced trying to hit 'More' after each defined title in dashboard.
Sorry for italian in the site but it's a user requirement.
getInfo having reference to your function. So you can call this by:
getInfo(value);
Use quotes if you are passing string value and if you are passing
integer value you can simply pass.
For instance:
getInfo("Message Title"); //string
getInfo(3); // Integer
DEMO
You can also call like this:
getInfo = function(title){
alert("maggiori_informazioni:" + title);
}("String");
DEMO
Updates:
You can use this:
"(<a href='"+getInfo(valore_comando)+";'>" +
the function has been assigned to getInfo so please call getInfo()
string should be enclosed with ''
So the code should be
getInfo('example')
You're going to have to escape some quotation marks.
"(<a href=\"javascript:getInfo('"+ valore_comando +"');\">" +
The \ character before the quotation marks for the href attribute tells the JavaScript parser that they're part of the string instead of the beginning or end of a string.
The problem is that example is not defined. If you wanted to call the function with example as input it needs either single quotes or double quotes around it. "example" or 'example'.
getInfo("example"); // Works as intended
Another option is to create a variable containing the string, and pass that to the function.
var myVariable = "example";
getInfo(myVariable); // Still works, myVariable is a string with value "example"
I checked a lot on Hrefs but couldn't get something related.
I am trying to do this in code behind which is actually a custom control class
writer.Write("<a href='javascript:document.location.href?" + filter.ParameterName + "=" + filter.QueryValue + "'>" + filter.UserVisibleValue + "</a>| ");
now this gets me something like this on hover of above anchor 'document.location.href?Test one=2013' and when i click it, this throws an obvious javascript error 'SyntaxError: missing : in conditional expression' because it takes it as a conditional operator and hence finds : missing.
I simply want that document.location.href (current url) should be calculated and the value put in where i use it.
I know that i may simply call a javascript function and inside that function i set the href but can i do it this way?
Try this:
writer.Write("<a href='javascript:window.location = document.location.href?" + filter.ParameterName + "=" + filter.QueryValue + "'>" + filter.UserVisibleValue + "</a>| ");
Note that you might have to escape values as needed otherwise JavaScript will become invalid. To prove that above approach works, you can copy-paste following simpler example in any HTML page and see it working:
bla
When I use below notation with '>' in line
$('#tablesorter > tbody')
in code
var message;
myService.getUsers({ callback : function(str) {
message= jQuery.parseJSON(str);
}});
$.each(message, function() {
$('#tablesorter > tbody').append(
'<tr><td>' + this.name
+ '</td><td>' + this.surname
+ '</td>' + '</tr>');
});
Note: myservice is a dwr service, don't know if it is related
I have error in chrome console
Uncaught Error: Syntax error, unrecognized expression: >
> means >
When I deleted the > the error disappears but it does not work as expected
the greater than > symbol is used in CSS selectors (which jQuery selectors are based on) to indicate a "direct descendent".
Documentation
The code you posted works fine, the > symbols in there would not cause any errors.
Wherever you're outputting your code, it's getting transformed to automatically escape. Perhaps you have a templating language in between.
You need to either store all of your javascript code in a separate file (the best solution), or find a way with your pages (JSP?) to disable output escaping.
I have the following javascript:
function getMessageFromXML(xml) {
alert('xml: ' + xml);
alert("Text of message: " + $(xml).find('dataModelResponse').find('adminMessage').text());
return $(xml).find('dataModelResponse').find('adminMessage').text();
}
which is being executed on the following XML:
<dataModelResponse>
<adminMessage>BARF</adminMessage>
<adminData>
<identifier>123456</identifier>
</adminData>
</dataModelResponse>
I know that the XML is correctly passed in, because of the first alert, but the message is showing up as blank for some reason. I verified that there were exactly 1 message and 1 dataModelResponse elements in the xml, using the .length modifier for similar find() queries, but for some reason, I can't get it to print out the correct message.
Suggestions?
EDIT: Changed the tag name I was searching for. Posted in between two revisions, sorry.
Replace $(xml).find('dataModelResponse').find('message').text(); with $(xml).find('message').text();.
The documentation for jQuery.find() states:
Get the descendants of each element in the current set of matched elements, filtered by a selector.
The root level element of your XML block is dataModelResponse. By calling $(xml).find('dataModelResponse'), you are essentially asking for a dataModelResponse within your dataModelResponse.
After $(xml) you’re already on the root node, which is dataModelResponse. Thus you will not find any child-elements of type dataModelResponse, and thus text() will return nothing.
Concrete:
console.log("Text of message: " + $(xml).text());
Will log
Text of message: BARF 123456
And (this is what you want)
console.log("Text of message: " + $(xml).find('message').text());
will log
Text of message: BARF
And
console.log("Text of message: " + $(xml).find('dataModelResponse').text());
will log
Text of message:
Should it be 'adminMessage' instead?
If you are using jQuery 1.5, you can write:
alert("Text of message: " + $($.parseXML(xml)).find('adminMessage').text());
or
alert("Text of message: " + $($.parseXML(xml)).find('dataModelResponse > adminMessage').text());
or
alert("Text of message: " + $($.parseXML(xml)).find('dataModelResponse').find('adminMessage').text());