I am trying to assign the following to a PHP variable:
[<a href='"'javascript:pop_booking_item_add({ID})'"'>Add New Item</a>]
I have tried this:
$add_button = "[<a href='"'javascript:pop_booking_item_add({ID})'"'>Add New Item</a>]";
But I am getting the error message:
Error: syntax error, unexpected ''javascript:pop_booking_item_a' (T_CONSTANT_ENCAPSED_STRING)
I think I need to escape the quotes but I'm not sure how to?
This should work:
$add_button = "[<a href='javascript:pop_booking_item_add({ID})'>Add New Item</a>]";
or
$add_button = "[Add New Item]";
" is a valid replacement for ' or " in mixed HTML / javascript. Have you tried with :
$add_button = "[<a href="javascript:pop_booking_item_add({ID});">Add New Item</a>]";
isn't it better to do it like this?
link
[].forEach.call(document.querySelectorAll('.classnameofyourlink'), function (el) {
el.addEventListener('click', function() {
// put your stuff here
// retrieve the {{ID}} like this: this.getAttribute('data-id'));
}, false);
});
Related
i know this question is so common but I don't know why I can't seem to fix this. most solutions were just fixing some misspelled words , misused symbols etc. pls help me. I kept having unexpected token }.
this is my code
<a href='#' id='editTmModal' data-toggle='modal' data-target='#editTMModal' onclick='showDialog(\'"+data+"\')'>"+data+"</a>
function showDialog(data,e){
var name=data;
alert(name);
}
pls. help me :(.
this code works on integer but not on String.
Assuming that this link-element is dynamically inserted into your website. Hope this helps:
var data = "23";
var linkElement = "<a href='#' id='editTmModal' data-toggle='modal' data-target='#editTMModal' onclick='showDialog("+data+",event)'>"+data+"</a>";
document.body.innerHTML = linkElement;
function showDialog(data,e){
var name=data;
alert(name);
alert(e);
}
You might find it easier to add event listeners in the JS, and use dataset.
I'm assuming that you're inserting the HTML into the document with JavaScript somehow, so my example reflects this.
const name = 'Bob';
const html = `Test`;
document.body.innerHTML = html;
function showDialog(){
let name = this.dataset.name;
console.log(name);
}
const edit = document.querySelector('#editTmModal');
edit.addEventListener('click', showDialog, false);
I want to pass a field of a domain object to a javascript function in my view.gsp (grails) , but I am getting a syntax error.
Here is my gsp and javascript - please let me know if you see the syntax error. Thanks!
/*HTML*/
<td>${fieldValue(bean: studentInstance, field: "active")}</td>
/*JS*/
<script type="text/javascript">
var id = 0;
function setID(userId){
console.log("userId: " + userId);
id = userId;
}
</script>
The issue is you have function in your onclick. You don't need it there. Remove it so your onclick looks like this:
onclick="setID( ${studentInstance.id})"
I am doing:
var url = '#Url.Action("Attachments", "Transactions")';
url += '/?id=' + 3201;
$("#attachments").load(url);
However, on load it doesn't do anything. Am i missing something?
I essentially want to call something similar to:
#{Html.RenderAction("Attachments", "Transactions", new { id = 3301 });}
I get the following error on console:
http://server:54137/Transactions/#Url.Action(%22Attachments%22,
You must be using an external JavaScript file which will not parse your razor syntax hence the error in your console of #Url.Action(%22Attachments%22..
You have a couple of options:
Create a JavaScript function and pass in the url:
function loadUrl(url) {
$("#attachments").load(url);
}
Then in your razor call it within a script tag:
loadUrl(#Url.Action("Attachments", "Transactions", new { id = #Model.Id })
Add the url to the html element as data and read it from your JavaScript with the data method.
In your razor markup add this:
<button data-url="#Url.Action("Attachments", "Transactions", new { id = #Model.Id })" />
From your JavaScript event handler read it with:
var url = $(this).data('url');
$("#attachments").load(url);
I prefer the second option.
You Need to use Html.Raw check below
var url = "#Html.Raw(Url.Action("Attachments", "Transactions"))";
url += '/?id=' + 3201;
$("#attachments").load(url);
Here is my script from CasperJS:
var casper = require('casper').create();
var url = 'https://example.wsw/';
casper.start('https://example.wsw/login.html', function() {
//this.echo(this.getTitle());
//this.download(url, 'google_company.html');
this.echo(this.getHTML('img#cpt_img', true));
});
casper.run();
The out put I get from this is:
<img id="cpt_img" src="/user/turing/image.asp?1394574424">
and this is what I am trying to get it the output:
/user/turing/image.asp?1394574424
so I can use this string above later when I need it.
Can anyone show me away to change the string to what I need in the output?
You can try this:
var imgTag = this.getHTML('img#cpt_img', true);
var src = imgTag.match(/src="(.*?)"/)[1];
Modified from https://stackoverflow.com/a/1684206/3351720
Try using this:
this.echo(this.getElementAttribute('img#cpt_img', 'src'));
How Do I select the title of the context? I am suppose to select it to change the title of the page.
var handler = function(context){
document.title = //context's title
....
}
//sample context from ajax request
var context = '<!DOCTYPE html><html>\
<head><title>Hello</title></head>\
<body>\
Click Here<p>This is my sentence.</p>\
</body></html>';
.ajax{(
...
success: function(data){
handler(data);
}
});
EDIT: I forgot the doctype just incase it's necessary. The context was from an AJAX Request.
You can use regex to extract the title as well
var matches = context.match(/<title>(.*?)<\/title>/);
var title = matches[1];
Demo
Just discovered a way to do this, the non-regex way
title = $(context).filter("title");
console.log(title.html());
Demo
The second argument to the jquery $ function is the context
So you can try $("title",$(context)).text()
this should do:
var title = $(context).eq(0).text()
var handler = function(context){
$xml=$.parseXML(context);
console.log($($xml).find('title').text());
}
var context = '<html><head><title>Hello</title></head><body>Click Here<p>This is my sentence.</p></body></html>';
handler(context);
http://jsfiddle.net/MdvWq/
Please check http://jsfiddle.net/sethunath/UAt5p/
$(context)[1].innerHTML
returns the title
I don't know why, but nobody mention this. This is a popular method to search for elements in responses:
$(function() {
var context = '<html><head><title>Hello</title></head><body>Click Here<p>This is my sentence.</p>< /body></html > ';
alert($(context).filter("title").html()); //Use filter to get elements you want
alert($(context).filter("a").html()); //Will get HTML of that link (<a>)
alert($(context).filter("body > p").html()); //HTML of <p>
});
http://jsfiddle.net/DerekL/THdaC/2/