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);
Related
I have a link to add event to google calendar which is populated from a database, but the date is formatted yyyy-mm-dd, and the time hh:mm, and i cannot alter this, but google calendar will not accept.
Can anyone please help me use javascript and the 'replace' function to remove the'-' and ':' from the html please?
<a href="http://www.google.com/calendar/event?
action=TEMPLATE
&text=Tester12
&dates=2014-01-27T22:4000Z/2014-03-20T22:1500Z
&details=Oranges
&location=Newquay
&trp=false
&sprop=
&sprop=name:"
target="_blank" rel="nofollow">Add to my calendar</a>
many thanks.
Fetch the href link from tag and store it in a variable.
var linkStr = "http://www.google.com/calendar/event?action=TEMPLATE&text=Tester12&dates=2014-01-27T22:4000Z/2014-03-20T22:1500Z&details=Oranges&location=Newquay&trp=false&sprop=&sprop=name:";
var re = /&dates=.*?&/g;
var result = re.exec(linkStr);
if(result!=null){
var replaceStr = result[0].replace(/[-|:]/g,'');
var finalLink = linkStr.substr(0,result["index"]) + replaceStr + linkStr.substr(result["index"]+replaceStr.length);
console.log(finalLink);
}else{
alert('link invalid');
}
This will remove all the '-' and ':' from dates parameter string and will store that link in 'finalLink' var.
Hope it helps.
I have been on the sniff for the whole code solution, and witha bit of mix and match, came up with this, AND IT SEEMS TO WORK!!!!!! But please feel free to edit into perfection!
<script>
var linkStr = "http://www.google.com/calendar/event?action=TEMPLATE&text=Example Event&dates=2018-12-16T10:3500Z/2018-12-16T12:0000Z&details=Trip to town&location=No mans land&trp=false&sprop=&sprop=name:";
var re = /&dates=.*?&/g;
var result = re.exec(linkStr);
if(result!=null){
var replaceStr = result[0].replace(/[-|:]/g,'');
var finalLink = linkStr.substr(0,result["index"]) + replaceStr + linkStr.substr(result["index"]+replaceStr.length);
console.log(finalLink);
}else{
alert('link invalid');
}
</script>
Add Event
<script>
(function() {
Array.prototype.forEach.call(document.querySelectorAll("a.finalLink"), function(link) {
link.href = finalLink;
});
})();
</script>
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);
});
Actually I've found a similar questions and neither of the answers helped me.
Could someone please help me figuring out how to remove <b> from a variable in js?
What I have is the following:
var p_name_original = $(this).find('td#p_name').html();
alert(p_name_original);
which returnes some text in bold, like <b>text</b>.
How to remove this <b> and </b> from p_name_original? It'd be much better if I could remote it while assigning the value to p_name_original.
Thanks in advance,
AshotAr.
simple: change .html() to .text()
If there are only <b> Tags use this:
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText;
}
If there are others to:
function strip(html)
{
html = html.replace(/<b>/g, "");
html = html.replace(/<\/b>/g, "");
return html;
}
try this
var p_name_original = $(this).find('td#p_name>b').html();
alert(p_name_original);
First, hello everyone as I'm new here.
To summarize my problem, I read the content of an XML file to display in a table.
The basic function to do this works well, I created a derivated function to include a search filter related to an input field.
The search algorithm works well and I'm able to preview the HTML code of the search result using the alert() function and this code seems proper and can be displayed in a browser properly as it would be supposed to. However the innerHTML code of the concerned div is not updated...
I would appreciate any kind of input or solution anyone could provide as I've been stuck on this ! Thanks !
Here is the code :
function printListMod2(){
//Search parameters ?
var searchContent = document.getElementById("searchField").value;
var i=0;
newHTML = "<table id=\"tableInstrus\">";
for (i=0;i<listInstrus.length;i++){
filename = returnFilename(i);
title = returnTitle(i);
tempo = returnTempo(i);
sample = returnSample(i);
multi = returnMulti(i);
style1 = returnStyle1(i);
style2 = returnStyle2(i);
var regEx = new RegExp(searchContent, 'gi');
var resultSearch = title.match(regEx);
if(resultSearch!=null){
if(i%2==0){
newHTML += "<tr class=\"tr0\"><td class=\"idColumn\">"+(i+1)+"</td><td class=\"emptyColumn\"></td><td class=\"nameColumn\">"+title+"</td><td class=\"tempoColumn\">"+tempo+"</td><td class=\"sampleColumn\">"+sample+"</td><td class=\"multiColumn\">"+multi+"</td><td class=\"styleColumn\">"+style1+"</td><td class=\"styleColumn\">"+style2+"</td><td class=\"addLink\"><a id="+filename+" onclick=\"addLinkToPlaylist("+i+")\"><img title=\"Add to playlist\" class=\"addButton\" src=\"images/buttonAdd.png\"/></a></td><td class=\"playLink\"><a onclick=\"playTrack("+i+","+true+")\"><img title=\"Play this track\" class=\"playButton\" src=\"images/buttonPlaySmall.png\"/></a></td></tr>";
}
else{
newHTML += "<tr class=\"tr1\"><td class=\"idColumn\">"+(i+1)+"</td><td class=\"emptyColumn\"></td><td class=\"nameColumn\">"+title+"</td><td class=\"tempoColumn\">"+tempo+"</td><td class=\"sampleColumn\">"+sample+"</td><td class=\"multiColumn\">"+multi+"</td><td class=\"styleColumn\">"+style1+"</td><td class=\"styleColumn\">"+style2+"</td><td class=\"addLink\"><a id="+filename+" onclick=\"addLinkToPlaylist("+i+")\"><img title=\"Add to playlist\" class=\"addButton\" src=\"images/buttonAdd.png\"/></a></td><td class=\"playLink\"><a onclick=\"playTrack("+i+","+true+")\"><img title=\"Play this track\" class=\"playButton\" src=\"images/buttonPlaySmall.png\"/></a></td></tr>";
}
}
}
newHTML += "<tr><td class=\"idColumn\"></td><td id=\"emptyColumn\"></td><td class=\"nameColumn\"></td><td class=\"tempoColumn\"></td><td class=\"sampleColumn\"></td><td class=\"multiColumn\"></td><td></td><td></td></tr>";
newHTML += "</table>";
alert(newHTML); //this displays the HTML code properly
document.getElementById("listDiv").innerHTML = newHTML; //this doesn't seem to do anything...
}
Is your script being executed before the document has finished loading?
Then it won't find #listDiv because the div doesn't exist yet.
I would check the javascript console output for errors and check if the following code doesn't return undefined:
{
...
console.log( document.getElementById('listDiv') );
}
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/