Get JS Variable in Ajax Response [duplicate] - javascript

This question already has answers here:
Executing <script> injected by innerHTML after AJAX call
(12 answers)
Closed 8 years ago.
I have an ajax response which returns html and javascript as follows:
<div class="contents">
<div>1</div>
<script>
var test = "test";
</script>
</div>
How can I access the variable test?

Append the response in some temp div, get the value from variable and remove the div.
$("<div id='dvTemp'></div>").html(AjaxResponse).appendTo("body");
alert(test);
$("#dvTemp").remove();

Related

Can I make Javascript inside Javascript? [duplicate]

This question already has answers here:
Can scripts be inserted with innerHTML?
(26 answers)
script tag create with innerHTML of a div doesn't work
(3 answers)
Closed 3 years ago.
I will make javascript inside javascript like this code.
<p id="div"></p>
<p id="jancuk"></p>
<script>
document.getElementById("jancuk").innerHTML = '<script
src="encryption.js" type="text/javascript">' + '</script>';
</script>
<script>
document.getElementById("div").innerHTML = "text show"; </script>
Code can't work correctly.
I will include data javascript 1 to javascript 2. it's mean wrapper inside one html document.
Please help me if you know about this.

why object "this" work in first situation and doesn't work in another, why it "undefined" [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 6 years ago.
first situation:
<span id="username" onclick="alert(this.innerHTML);">[Василий]</span>
after click on text "[Василий]" i see text in dialog window "[Василий]". OK. But in second situation when I'd written code below I saw "undefined", why?
<script type="text/javascript" charset="utf-8">
function func() {
alert(this.innerHTML);
}
</script>
<span id="username" onclick="func();">[Василий]</span>
Basically you have no reference to the object, you clicked on. You could take this as parameter and get the value in the function.
function func(t) {
alert(t.innerHTML);
}
<span id="username" onclick="func(this);">[Василий]</span>

.on("click",...) not working on objects created with the same on click function [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
jQuery function not binding to newly added dom elements
(13 answers)
Closed 6 years ago.
since I am new to JS / jQuery it is probably a obvious problem (but no exception thrown). I read that I can't use .onclick so i tried this approach with .on("click"...)
I try to build a table that adds new lines under the clicked line. This should also work for the newly created lines.
My html:
<tbody>
<tr id="line1"><td>Stuff1</td><td>Stuff</td><td>Stuff</td><td>Stuff</td><td>Stuff</td></tr>
<tr id="line2"><td>Stuff2</td><td>Stuff</td><td>Stuff</td><td>Stuff</td><td>Stuff</td></tr>
<tr id="line3"><td>Stuff3</td><td>Stuff</td><td>Stuff</td><td>Stuff</td><td>Stuff</td></tr>
<tr id="line4"><td>Stuff4</td><td>Stuff</td><td>Stuff</td><td>Stuff</td><td>Stuff</td></tr>
</tbody>
My JS:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("tr").on("click",function(){
var childId = $(this).attr("id")+1;
if($("#"+childId).length){
$("#"+childId).toggle();
}else{
$(this).after('<tr id="'+childId+'"><td>Stuffxxxx</td><td>Stuff</td><td>Stuff</td><td>Stuff</td><td>Stuff</td></tr>');
//AJAX
}
});
});
</script>
Thanks for your help! :)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("table").on("click","tr", function(){
var childId = $(this).attr("id")+1;
if($("#"+childId).length){
$("#"+childId).toggle();
}else{
$(this).after('<tr id="'+childId+'"><td>Stuffxxxx</td><td>Stuff</td><td>Stuff</td><td>Stuff</td><td>Stuff</td></tr>');
//AJAX
}
});
});
</script>

C#: Using HTMLAgilityPack how to get a value inside a script tag [duplicate]

This question already has an answer here:
Parsing HTML to get script variable value
(1 answer)
Closed 7 years ago.
I am using HTMLAgilityPack. I want to get a value inside a script tag, see the code:
<div id="frmSeller" method="post" name="frmSeller">
<div class="clear"></div>
<script type="text/javascript" language="javascript">
Biz.Product.GroupItemSwitchLogic.init();
Biz.Product.GroupItemSwitcher.init({
properties:null,
availableMap:[{"info":{"item":"28-200- 286","price":95.99,"vehicle":null},"map":[]}],
selectedProperties:[]
});
</script>
</div>
From there I want to get value of "price" that is 95.99.
How can i get this kindly tell me, what type of Regex I can use....
Thankyou
You can do some string manipulation like this
s = the html code
var s = z.Split(new String[] { #"price"":" }, StringSplitOptions.None);
var price = s[1].Split(',')[0];
and now price variable has your price

javascript document.getelement not working [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 8 years ago.
I am very new to javascript. I am trying to place value on one input field. But it is not working, I don't know why..
document.getElementById("c_add").value='sssssss';
in the text area with id "c_add", I supposed that value will place as "sssssss", but it is not setting any value..
Full Code:
<body>
<script type="text/javascript">
document.getElementById("c_add").value='sssssss';
</script>
<textarea name="c_add" id="c_add"></textarea>
</body>
Did you tried do like this?
<input type="text" id="mytext">
<script type="text/javascript">
var elem = document.getElementById("mytext");
elem.value = "My default value";
</script>

Categories

Resources