jQuery doesn't give a proper output, but gives these [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
(function() {
var a = document.createElement('a');
var mref = 'http://www.someprivatelink.net';
var linkText = document.createTextNode(title1);
var title1 = $('#node-264152').find('.audio-description').html();
alert(title1);
a.appendChild(linkText);
a.title = title1;
a.href = mref;
document.body.appendChild(a);
})();
I'm using this script to add an download link to the bottem of my page wich doesn't to to be permanent so I enter it via console with Google Chrome.
The ID and the Class are correct to the value I need to extract, I've added the alert to check if the fault was in the HTML or in my javascript.
The output at the bottem of the page has the value of 'Undefined', while it has to be 'Log moving along in body of water 2'
But the alert does display (screenshot) this message with a few boxxes before and after the message, I think this is causing the undefined error.
Is there any solution my var can be converted into only text?
Thanks in advance, Tjalle

You're trying to use title1 before it's defined. swap the lines
var title1 = $('#node-264152').find('.audio-description').html();
var linkText = document.createTextNode(title1);
Should be noted that since you're already using jQuery, you could do
var title1 = $('#node-264152').find('.audio-description').html();
$('<a />', {
href : 'http://www.someprivatelink.net',
title : title1,
text : title1
}).appendTo('body');

Related

add a variable in href using javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am new in javascript i want to add a javascript variable in
i use some javascript like this but it not working
HTML
<ul id="direction"></ul>`
JS
var abc = 'link/';
var cba = 'hello';
document.getElementById('directions').innerHTML = 'Link';`
This code should work, make sure the html id matches the getElementById and use the right quotes as mentioned before.
You want to add a href to a list but you should add a li first, in my example I use a div tag.
var name = 'google';
var ext = '.com';
document.getElementById('direction').innerHTML = 'Link';
A better way to do this would be by using backticks `` < these like so:
var name = 'google';
var ext = '.com';
var link = 'Link';
document.getElementById('direction').innerHTML = `${link}`;
Your code, fixed ID, and quotes:
document.getElementById("direction").innerHTML='Link'
Or maybe better:
var node=document.createElement("A")
node.setAttribute("href","https://"+var1+var2)
document.getElementById("direction").appendChild(node)
I hope that this will help you!
I'd recommend avoiding using innerHTML in most cases! The following accomplishes what you want programmatically without needing to generate HTML strings:
window.onload = () => {
let abc = 'link/';
let cba = 'hello';
let container = document.getElementById('direction');
let link = container.appendChild(document.createElement('a'));
link.setAttribute('href', `https://${abc}${cba}`);
link.appendChild(document.createTextNode('Link'));
};
<ul id="direction"></ul>

Problem adding Js variables in HTML with getElementById [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
<body>
<h1>Operadores aritméticos</h1>
<h3>+ Adición (102+103)=</h3><p id="suma1"></p>
<h3>- Substracción</h3><p id="resta1"></p>
<h3>* Multiplicación</h3><p id="multi1"></p>
<h3>/ División</h3><p id="div1"></p>
<h3>% Módulo</h3><p id="mod1"></p>
<script>
var suma = (102+103);
var resta = (36-20);
var multiplicación = (27*30);
var división = (900/30);
var módulo = (106%3);
document.getElementById("suma1").innerHTML = suma;
document.getElementById("resta1")innerHTML = resta;
document.getElementById("multi1")innerHTML = multiplicación;
document.getElementById("div1")innerHTML = división;
document.getElementById("mod1")innerHTML = módulo;
</script>
Hello guys, I have a problem, im pretty new at this (programming with HTML, Js, etc.).The issue is that when I try to make my Js variables appear on HTML (with document.getElementById), they do not appear. Nevertheless, if erase every document.getElementById except the one containing "suma1", the browser displays me the result of the sum (205), but if I add even one of them, the browser doesn´t display anything.
I hope I was clear with my problem, it seems very simple but hard to explain.
Any suggestions?
Thanks in advance
The reason you're not seeing anything when you add the statements to populate resta1, multi1, div1, and mod1 is probably because they all have a syntax error. This is likely causing even the first statement (suma1, which is syntactically valid) not to work.
Valid Statement
The 1 statement that is working is document.getElementById("suma1").innerHTML = suma;
Invalid statements
All the other statements follow this pattern:
document.getElementById("id")innerHtml = variable;
Note that you're missing the . between getElementById("id") and innerHtml. If you add the missing . then it should all work as expected.

Why do I get an error when trying to console log my $.each [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Sorry im new to jquery I usually use php/html. Can you tell why my code wont console log the VAR values. I am building an array from my vars when looping through them. I get the error Uncaught ReferenceError: $ is not defined.
Fiddle
var string1 = "tes£$%t";
var string2 = "test";
var string3 = "test";
var string4 = "test";
var check_fields = [string1, string2, string3, string4];
$.each(check_fields, function(index, value) {
if (value.replace(/^[a-z\d\-_\s]+$/i, "") != string) {
console.log(value);
}
});
Your code is fine but jquery file is missing here.
Fiddle link:- In the left side under the external resource just put the jquery cdn path and click on run then check.
https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js
This will resolve your problem.
include the jquery header file in the head your html code
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
Your html file is missing the external jQuery library.
Try adding this to the start of your html file (preferably in the head, of in the case of JSFiddle just paste it into the HTML box):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
Let me know if this works

how to find and replace text url with links in jquery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Hi I want to find text url in my editor and convert them to anchor tag links using jquery.
here is my code
function urlify(text) {
var urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
return text.replace(urlRegex, function(url) {
return '' + url + '';
})
}
$("#queEditor").bind('input propertychange', function(){
var url = urlify($("#queEditor").text);
console.log(url);
});
But it's giving an error undefined function text. Could someone help me rectify this?
Since .text() is a function you need to use () when you want to invoke it.
$("#queEditor").text(); //Notice ()

Why doesn't .attr("src", "/some/path/image.png") work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to replace the src of an tag with another path. This works perfectly well:
var tempDocId = 'someId';
$('#documents' + that.ticketId).append('<img id="'+tempDocId+'" src="/img/support/pdf_icon.jpg">');
but the following code gives one of those "image not found" icons ():
var tempDocId = 'someId';
$('#documents' + that.ticketId).append('<img id="'+tempDocId+'" src="/img/support/loading.jpg">');
$('#tempDocId').attr("src", "/img/support/pdf_icon.jpg");
Does anybody know what I'm doing wrong here? All tips are welcome!
because you are looking for id="tempDocId", not the one you generated.
$('#tempDocId').attr("src", "/img/support/pdf_icon.jpg");
needs to be
$('#' + tempDocId).attr("src", "/img/support/pdf_icon.jpg");
so you are not replacing the source. My guess is that your loading image is not valid.
Because $('#tempDocId') doesn't exist. tempDocId is a variable, so try:
$('#' + tempDocId).attr('src', '/img/support/pdf_icon.jpg');
var tempDocId = 'doc' + Math.random().toString().substr(2);
var $img = $('<img id="'+tempDocId+'" src="/img/support/loading.jpg">');
$img.appendTo('#documents' + that.ticketId);
$img.attr('src', '/img/support/pdf_icon.jpg');

Categories

Resources