Jquery load() use variable instead of a url [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I use the jquery to load a page into a div. like:
#('#mydiv').load("http://gowemto.com/index.php");
I want to store the url into a variable like this:
var myload="http://gowemto.com/index.php";
but when I try to call this function #('#mydiv').load(myload");
it doesn't work :(
any help? I really need to use variable instead of url inside my function

There is a typo, you have to remove the extra ", at the end of the variable myload; try:
$('#mydiv').load(myload)
if you are storing jQuery in # var (eg noConflict), but I don't think so, your code must be:
#('#mydiv').load(myload)

You have a syntax error (extraneous " after the variable name), and you should use $ instead of #:
#('#mydiv').load(myload");
It should be:
$('#mydiv').load(myload);

Related

how to pass a variable to queryselectorAll? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.

Using innerHTML for partial HTML [closed]

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 6 years ago.
Improve this question
the following function
<script>
function new_par()
{
var beginning="<input id=";
document.getElementById("debug").innerHTML=beginning;
}
</script>
produces no output, although if I remove the "<" sign, it does. Presumably the javascript avoids an output that would destroy the html page, but would there be anyway to force the output?
You're assigning it to a node's innerHTML property. When you plug the string into the DOM it's trying to parse it as though it were HTML. If you were to simply add it as a text node it would appear.
var beginning="<input id=''";
document.body.textContent = beginning;
https://jsfiddle.net/2patzyo1/
Edit: upon looking again at your question, if you are trying to get the input element to appear on the page, the string you're using isn't proper HTML because you never closed the tag. When the browser runs into it, it tries to parse and render it, but because it isn't valid it gives up. This will work:
var beginning="<input id=''>";
document.body.innerHTML = beginning;
https://jsfiddle.net/2patzyo1/1/

add and remove html elements to jquery variable [closed]

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
i have a JS variable like this:
var elmnts = $('#elm1, elm2, #elm3, #elm4')
how can i add and remove other html elements?
i try this code but there is no result.
elmnts.add('#elm5');
elmnts.remove('#elm1')
$.fn.add returns new collection, it doesn't modify original one. So you should use this syntax.
elmnts = elmnts.add('#elm5');
To remove element from a collection of jQuery objects you can use $.fn.not method:
elmnts = elmnts.not('#elm1');
You should not use remove for this, it's used to delete element from DOM tree.

Get the value of a div [closed]

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 9 years ago.
Improve this question
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

I have one javascript error in my IE8 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
"Object doesn't support this property or method"
It's this line.
pthumb = $("#pthumb").attr("src");
Does anyone know why?
You have a javascript variable called "pthumb" and a DOM element with the id "pthumb", and IE's JS engine could be trying to use the wrong one.
If you have a function also called "pthumb" then IE could also be trying perform this action on the function object.
The last thing to try is to make sure you are using "var" when declaring "pthumb" in the Javascript. i.e.:
var pthumb = $("#pthumb").attr("src");
Are you ensuring the DOM is ready?
$(document).ready(function(){
//wrap your code in document-ready check
pthumb = $("#pthumb").attr("src");
});
you could double check the plain javascript method:
var jthumb= document.getElementById('pthumb').attributes['src'].value;
try{
pthumb = $("#pthumb").attr("src");
}
catch(er){
alert(er.message + '\n'+jthumb)
}
If you don't catch the error, the element is not yet ready.

Categories

Resources