Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I think my concatenating is fine, but my use of it isn't working. Any help would be appreciated. Thanks.
<script>
var str1 = "/images/";
var str2 = "test.jpg";
var res = str1.concat(str2);
</script>
<div class="product-sprite" data-image="res"></div>
DOM element attributes are not evaluated as variables. You need to assign it to the element property.
var res = str1 + str2; // no need to use .concat()
document.querySelector(".product-sprite").dataset.image = res;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a dropdown showing list of languages,
Now, some of these languages contains special HTML characters, but I want to show the real values of those characters.
Like.
è should be replaced with è
How to do this using Javascript ?
Thanks in advance !!
One way to do this:
const decodeHtml = (html) => {
const t = document.createElement("textarea");
t.innerHTML = html;
return t.value;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am getting some data from my server and I want to create element but this code is not working please help me
var filelist=['dummy1','dummy2','etc']
filelist.forEach(file=>{
var newli=document.createElement("li")
var newa=document.createElement("a")
newa.innerHTML=file
newa.setAttribute('href',file)
newli.append(newa)
box.append(newli)
})
Note the box is a div in above code
Here you go, this is working. One needs to assure that before the execution of any DOM accessing functionality, the former has to be ready/loaded.
const fileNameList = ['dummy1', 'dummy2', 'etc'];
const box = document.getElementById('box');
fileNameList.forEach(fileName => {
const newLi = document.createElement("li");
const newA = document.createElement("a");
newA.innerHTML = fileName;
newA.setAttribute('href', fileName);
newLi.append(newA);
box.appendChild(newLi);
});
<div id='box'>Your box</div>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to receive get parameter from the current page to do xhr request further. I've tried to set data- tags but I think this kludge is not a good deсition.
Give this a try
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How to add var in get value in JavaScript ?
I have 2 var
var page = $('#demoajax').val();
var name = $("#users").val();
This is mym old code
data:"server=Windows&actionfunction=showData&page="+page,
I want to add var name and value now=date to this line how can I do that ?
First I try this
data:"username="+name+"&server=Windows&actionfunction=showData&page="+page+"&now=date",
But not work.
You appear to have inserted your changes in the middle of the query string. Try this instead.
data:"server=Windows&username="+name+"&actionfunction=showData&page="+page+"&now=date",
Why do you have data:"username="+name+"&server=Windows&actionfunction=showData&page="+page+"&date=now",
switch date and now
Also lose the comma at end if not needed
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm not so good with regex or trying to return the side part of a string. Can someone help me figure this out. I have a demo below.
str = "<html><head><script>var x = '123';</script></head></html>";
console.log(str)
// should return var x = '123';
Someone wrote a very good regex for stripping tags:
var strippedStr = str.replace(/(<([^>]+)>)/ig,"");
console.log(strippedStr);
Source: http://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
I played around a bit with it and found a way with match using groups:
str.match(/(>)([^><]+)(<\/)/m)[2]
result = "var x = '123';"
=> a range (2nd group) beginning by ">" (1st group) and ending with "var x = '123';
I am not sure it'll cover all the cases...