Fill input with jquery variable (Input receive html code) - javascript

I am able to open an external page in the DOM.
But I am trying to fetch all the child tags from a div and save it to the javascript variable and then print the variable in the input value
I need to save in the input all html content of the div and the child tags:
<form method="POST" action="test.php">
<input id="content" type="text" name="content" value="<script>document.write(content)</script>">
<input type="submit" value="submit">
</form>
<div id="mydiv"></div>// here load external page
<script>
$(document).ready(function(){
$("#mydiv").load("index.html"); // load external page in Dom
var content = $(this).closest('#div1');
/* find all parents tags e content of div id="div1"
(This is inside the external index.html file) */
});
</script>
I'm not getting it, I do not know where I'm going wrong in the code above
Thanks

//try like below
<form method="POST" action="test.php">
<input id="content" type="text" name="content" value="">
<input type="submit" value="submit">
</form>
<div id="mydiv"></div>// here load external page
<script>
$(document).ready(function(){
$("#mydiv").load("index.html");
var loadFile = load("index.html");
var content = $(loadFile).find('#div1').html();
$('#content').val(content);
});
</script>

you need to use callback function to view/explore the loaded contents using $.fn.load
$(document).ready(function(){
$("#mydiv").load("index.html", function(data){
// assuming #div1 is inside "index.html"
var content = $(data).find("#div1");
});
});

Related

Javascript password + button open URL in targeted element

I learned how to open a link using a password and button.
I used the following code:
<input type="text" id="text" />
<input type="button" id="btn" value="Submit" onClick="window.location=url;" />
<script>
$("#btn").click(function() {
window.location="example.com/"+$("#text").val();})
</script>
What I would like to figure out is whether I can target a div element for the link to open within.
I've been using the following script to load html pages into a specified div element with a link:
<div class="item1" id="pageone"></div>
link
<script>
function loadSAMEDiv1() {
$.ajax({
url: 'item1.html',
success: function(data) {
$('#pageone').html(data);
}
});
}
</script>
I guess I'm wondering if there's a way to make the password url made by the first script open in a targeted div, like it does in the second script. So when you click the button element it will make the url using the password entered into the text box and load it into a div like #pageone.
The simplest way to do this is with Ajax load(). Try this.
<input type="text" id="text" />
<input type="button" id="btn" value="Submit"/>
<div class="item1" id="pageone"></div>
<script>
$("#btn").on("click", function() {
$("#pageone").load("example.com/"+$("#text").val());
});
</script>

HTML input into Javascript variable

I've been attempting to grab text input from HTML and trying to output the text back into HTML but for some reason it does not seem to be working. Does putting input within a form alter the interaction with getElementById or is it possible that the second button usage of formaction is interrupting this?
$(document).ready(function(){
$("#submit").on("click",function(){
var test = document.getElementById("searchInput");
$("#text").html(test);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
<div id = "text">
<h1>test</h1>
</div>
EDIT: I have attempted to change
var test = document.getElementById("searchInput");
to
var test = document.getElementById("searchInput").value;
previously and it is still not working. Is there an interaction that is failing that I am missing? Also I would like to be able to store the input as variable i.e thats why I am purposely putting it into a variable before outputting it.
EDIT2: There wasn't anything particularly wrong about the code other than retrieving the value. I am currently using codepen.io to code and did not load jquery into the pen.
Since you're using jQuery, it will make more sense to use jQuery to obtain the elements' value:
Use this to always replace #text element contents with #searchInput value:
$("#text").html($("#searchInput").val());
Use this to append #searchInput value to #text element:
$("#text").append($("#searchInput").val());
But the whole logic does not make any sense. You are changing an elements' value that it is outside the form you are submitting. Either you should prevent submit button click event from submitting the form, or have the #text element inside the form as an input, textarea or hidden field:
$(document).ready(function(){
$("#submit").on("click",function(e) {
e.preventDefault();
$("#text").html($("#searchInput").val());
})
});
or having the #text element inside the form for submitting:
<form>
<input type="hidden" id="text" name="text">
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
You Using form element, so once you click the button , the form will be reloaded, if you want to display textbox value to the element , once remove the form element.
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
<div id ="text">
<h1 id="welc"></h1>
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#submit").on("click",function(){
var test = $('#searchInput').val();
$("h1").html(test);
});
});
</script>
Just Remove tag, and check it.
You need to find the element's value and then set it as HTML.
$("#text").html(test.value);
I have updated your question mate check this one .
$(document).ready(function(){
$("#submit").on("click",function(){
var test = document.getElementById("searchInput").value;
$("#text").html(test);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
<div id = "text">
<h1>test</h1>
</div>
I think this will work for you.
<! doctype HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<input type="text" id="searchInput">
<br>
<button type="button" id="submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
<div id="text">
<h1>test</h1>
</div>
</body>
<script>
$(document).ready(function () {
$("#submit").on("click", function () {
$("#text").html($("#searchInput").val());
})
});
</script>
</html>
Edit your JS code please try:
EDIT:
$(document).ready(function(){
$("#submit").on("click",function(){
var test = $("#searchInput").val();
$("#text").html(test);
})
});

Loading a html page from other page and prefilling the values in textboxes in the catered pages

I got stuck in a problem.
Problem is:
I have to load a html page(say test.html) on click of a button from some page(say home page).
Now test.html contains some input boxes which I need to prefill while loading the test.html.
Content of home page
<body>
<h1> Page 1 </h1>
<div id="topBar">
HOME
</div>
<div id ="content">
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
document.getElementById("content").innerHTML='<object type="text/html" data="test.html" ></object>';
document.getElementsByClassName('monday_spends').setAttribute("value", "yolo");
});
</script>
test.html contains
<table class="ss-moneyDetails">
  <tr>
    <th>Details:</th>
  </tr>
<tr>
<td>Monday</td>
<td><i class="ss-iconRupee">₹</i><span>
<input class="monday_spends" type="text" value="" name="monday_spends"/>20,000</span></td>
</tr>
</table
I need to prefill the value of <input class="monday_spends" type="text" value="" name="monday_spends"/> while loading the page test.html from home.html.
NOTE : I am also trying to do this by calling a ajax request which will cater the html page and prefilling the data. Is it possible ?
Am I missing something?
You cannot insert JavaScript inside an <object> which you embed on your webpage.
However, you can rely on constructing the URL of the page you're embedding, to pre-fill certain inputs. For example:
$(document).ready(function(){
var money = 500;
document.getElementById("content").innerHTML =
'<object type="text/html" data="test.html?money_spend='+ money +'" ></object>';
});
I have done this using this process.
I have created an input tag of hidden type and embed it in form tag in test.html.
<form action="file:///Users/XYZ/Desktop/abc/home.html" method="GET">
<input type="hidden" id="foo" name="foo">
<input type="submit" value="Send" name="submit" id="submit">
</form>
and in script section I have assigned a value to the foo as
$(document).ready(function(){
var data = {};
data.name = "Mukul";
data.place = "gurgaon";
var data1 = JSON.stringify(data);
$("#foo").val(data1);
});
Now in home.html
<script language="JavaScript">
processForm();
function processForm()
{
var parameters = location.search.substring(1).split("&");
var temp = parameters[0].split("=");
data = unescape(temp[1]);
console.log("i got the data from test.html");
$("#tag1").val(data);
}
$()
</script>
I have retrieved the value from the url and appended it to the desired value.

How can I open a URL inside a DIV tag using AJAX?

I have the following php files. One that captures a user's text from a simple form, and the other echos this text. The files are shown below..
file1.php:
<form action="output.php" method="post">
Paste text document: <br><br>
<textarea name="doc" rows="5" cols="50" value="">
</textarea><br><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="Clear">
</form>
<br><br>
<div id="output_area">
</div>
</body>
</html>
output.php:
<html lang="en">
<head><title>Output</title></head>
<body>
<?php
$doc = $_POST['doc'];
echo $doc;
?>
</body>
</html>
I want the output to be displayed between the DIV tags, rather than to load and show the output on a new page. I want the text to be output in between the DIV tags on the same page. Maybe the best way is to use AJAX to display output.php within the DIV tags and output the text after the user has clicked on the "submit" button. How can I use AJAX to do this in the most basic way possible?
You can use JQuery ajax function:
JQuery:
$("form").on("submit", function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "output.php",
data: { doc: $('#doc').val()}
})
.done(function( data) {
$("#output_area").html(data);
});
});
Change html:
<textarea name="doc" id="doc" rows="5" cols="50" value=""></textarea>
You can use the function:
$(document).ready(function() {
$( "#output_area" ).load( "output.php" );
});
But remeber!
That the load function from jquery, loads everything on the page you are asking for, so dont use:
<html lang="en">
<head><title>Output</title></head>
<body>
</body>
</html>
Now the load(function), will put in the PHP you made in output.php
if you want to output the answer on a submit i would do it like:
First add:
Show doc
Then add jquery:
$(document).ready(function() {
$('.pressme').on('click', function(){
var yourdata = $(this).parent().find("textarea[name='doc']").val();
$.ajax({
type: "POST",
url: "output.php",
data: {doc: yourdata},
success: function(data) {
$('#output_area').html(data);
});
});
});
I added a <a>, instead of using the <input type="submit"> because the submit, will post the form, thats not what you need, after what i understand, you need to preview the textarea in a div.
Hope it helps!
If you want your $_POST variable to be shown on the same page, then
1.change form's action to it. In your case
action="file1.php" or leave it empty to reload the page on submit (not valid in HTML5)
2.insert echo part directly in your div
<div id="output_area">
<?php
echo $_POST['doc'];
?>
</div>
not in another file
You need not use ajax for this. You can do this by using javascript. It will improve your performance of your code.
Try This:
<script>
function show() {
document.getElementById("output_area").innerHTML=document.getElementById("doc").value;
return false;
}
</script>
<form action="#" method="post">
<textarea name="doc" id="doc" rows="5" cols="50" value="">
</textarea>
<input type="submit" name="submit" value="submit" onclick="return show();">
<input type="reset" name="reset" value="Clear">
</form>
<div id="output_area"></div>
</body>
</html>

E-mail form interactivity

I'm a web development student and I need some help. I have the code below; How do I make it work only when the form is submitted and not the text field is clicked. I also would like it to get and insert the textField's value in the .thanks Div. Please help me learn.
<script type="text/javascript">
$(document).ready(function(){
$(".quote").click(function(){
$(this).fadeOut(5000);
$(".thanks").fadeIn(6000);
var name = $("#name").val();
$("input").val(text);
});
});
</script>
<style type="text/css">
<!--
.thanks {
display: none;
}
-->
</style>
</head>
<body>
<form action="" method="get" id="quote" class="quote">
<p>
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
</form>
<div class="thanks"> $("#name").val(); Thanks for contacting us, we'll get back to you as soon as posible</div><!-- End thanks -->
This is a bit rough and ready but should get you going
$(document).ready(function(){
$("#submitbutton").click(function(){
//fade out the form - provide callback function so fadein occurs once fadeout has finished
$("#theForm").fadeOut(500, function () {
//set the text of the thanks div
$("#thanks").text("Thanks for contacting us " + $("#name").val());
//fade in the new div
$("#thanks").fadeIn(600);
});
});
});
and I changed the html a bit:
<div id="theForm">
<form action="" method="get" id="quote" class="quote">
<p>
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>
<label>
<input type="button" name="submitbutton" id="submitbutton" value="Submit" />
</label>
</p>
</form>
</div>
<div id="thanks">Thanks for contacting us, we'll get back to you as soon as posible</div><!-- End thanks -->
There are several things at issue here:
By using $('.quote').click(), you're setting a handler on any click event on any element contained within the <form>. If you want to catch only submit events, you should either set a click handler on the submit button:
// BTW, don't use an id like "button" - it'll cause confusion sooner or later
$('#button').click(function() {
// do stuff
return false; // this will keep the form from actually submitting to the server,
// which would cause a page reload and kill the rest of your JS
});
or, preferably, a submit handler on the form:
// reference by id - it's faster and won't accidentally find multiple elements
$('#quote').submit(function() {
// do stuff
return false; // as above
});
Submit handlers are better because they catch other ways of submitting a form, e.g. hitting Enter in a text input.
Also, in your hidden <div>, you're putting in Javascript in plain text, not in a <script> tag, so that's just going to be visible on the screen. You probably want a placeholder element you can reference:
<div class="thanks">Thanks for contacting us <span id="nameholder"></span>, we'll get back to you as soon as possible</div>
Then you can stick the name into the placeholder:
var name = $("#name").val();
$('#nameholder').html(name);
I don't know what you're trying to do with the line $("input").val(text); - text isn't defined here, so this doesn't really make any sense.

Categories

Resources