I am having trouble adding this function to my site. I have CustomPostTypes with Products and a simple form on the site, what I would like to do is on click storing a variable with the post title and appending it at the textarea in the form. ( https://imgur.com/a/205JCHJ how it should work ).
I am a complete beginner with jQuery and Javascript so I would really appreciate if someone can gimme a guide on how to do this
<h2 id="post-title">Cake name 2</h2>
<button id="btn" onclick="appendToTextArea()">WhatsMyName</button>
<textarea id="txt-area" name="textarea"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
function appendToTextArea()
{
var button_name = $("#btn").text();//post title
$("#txt-area").append(button_name);
}
</script>
Related
I am trying to create a hint Button that displays the text when it is clicked. I know how to create using javascript in HTML but I am having difficulty creating it in jquery. Can somebody please give me an idea.
javascript code in HTML:
<button type="button" onClick="myFunction()">Hint!</button>
<p id="hint"></p>
<script>
function myFunction() {
document.getElementById("hint").innerHTML = "<em><u>hint here.</u></em>";
}
</script>
Assuming you have only one button, you can bind the event click and use the selector #hint.
The function $.html is used to set HTML code to the selected elements.
$('button').on('click', function() {
$("#hint").html("<em><u>hint here.</u></em>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button">Hint!</button>
<p id="hint"></p>
Is there a way for JavaScript to find the page that an ID is on and return a link to that page?
For example, if I had a paragraph tag with id="test" on a page called test.html could I get a JavaScript variable to be set to a link to test.html because it has an id="test" on it.
I have probably explained this really badly but I was just wondering if it was possible.
Thanks.
Example of code on test.html
<p id="test"></p>
It is not perfect but I have found a way (kind of) to do this.
HTML:
<input id="tosearch" type="text">
<button id="search" type="button" onclick="getsearch()">Search IDs</button>
<p id="searchout"></p>
<script src="search.js">
</script>
<p hidden id=".id">examplepage.html</p>
<p hidden id=".id2">examplepage2.html</p>
JavaScript:
function getsearch() {
var search = document.getElementById("tosearch").value;
search = "." + search;
var page = document.getElementById(search).innerHTML;
page = page.link(page);
document.getElementById("searchout").innerHTML = page;
}
This allows you to search for an id and returns a link to the page with the id but you have to manually put all the ids and links in.
I was making an HTML code editor, I tested all of the HTML tags I know and they all work, except for script tags.
When I type <script>something</script> into the text area and click a button, the script doesn't execute.
Please help! Here is the code:
<span id="finishedProduct">
<p>When you enter code, your finished product will be here! Don't worry, if you make a mistake you can fix it later!</p>
</span>
<form name="userCode">
<textarea name="userCode" cols="90" rows="20" placeholder="Type your code here"></textarea></br>
<button type="button">Run Code!</button>
</form>
<script>
function makeCode() {
var userCode=document.forms["userCode"]["userCode"].value;
document.getElementById('finishedProduct').innerHTML = userCode;
}
</script>
Here is the working code:
<span id="finishedProduct">When you enter code, your finished product will be here! Don't worry, if you make a mistake you can fix it later!
</span>
<form name="userCode">
<textarea name="userCode" cols="90" rows="20" placeholder="Type your code here"></textarea>
<br/>
<button type="button" onClick="makeCode()">Run Code!</button>
</form>
<script type="text/javascript">
function makeCode() {
var userCode=document.forms["userCode"]["userCode"].value;
document.getElementById('finishedProduct').innerHTML = userCode;
}
</script>
Here's a link to the JSFiddle: http://jsfiddle.net/Q2qLF/. I've removed some broken HTML, such as; a button shouldn't be contained in a anchor tag, I've added a 'onclick' in your button that will call the 'makeCode()' function and I've added the 'type="text/javascript"' into your script tag as this maximises compatibility.
Please let me know if you need any more help
I've updated my JSFiddle http://jsfiddle.net/Xanco/Q2qLF/1/
Now there are 2 textareas, one for the HTML and one for the Javascript. i've also created a new function called 'makejs', this takes the value of the Javascript textarea and runs it through a 'eval' - this executes the Javascript passed to it.
I've put the answer in a Fiddle here: http://jsfiddle.net/joshnicholson/P8eh9/
I'm not sure why you're wrapping the button element inside an anchor, but I would do it a slightly different way.
Here is the revised javascript:
var myButton = document.getElementById("btnRunCode");
myButton.addEventListener("click", makeCode);
function makeCode() {
var userCode=document.forms["userCode"]["userCode"].value;
document.getElementById('finishedProduct').innerHTML = userCode;
}
I added an id of "btnRunCode" to your button element, just to make things easier for me. See the Fiddle.
I am trying to make a similar bit of code like at the bottom of this page to leave a comment. I have the basic code but the output does not register new lines (or HTML, but that isn't important). I have the function below called on key-up on the text field. Any help would be greatly appreciated. Thanks
Here is the whole page (Now working)
<html>
<body>
<form>
<textarea id="text" onkeyup="outputText()"></textarea>
</form>
<div id="outputtext" style="width:500px;">
</div>
</body>
<script type="text/javascript">
function outputText()
{
var text = document.getElementById('text').innerHTML;
document.getElementById('outputtext').innerHTML = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');
}
</script>
</html>
document.getElementById('outputtext').innerHTML = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2')
Have you tried getting the textarea contents as
var text = document.getElementById('text').value; instead?
I think it's good for you to take a look at how tools like jQuery can make your live easier in this kind of cases. Your particular question is a bit unclear however...can you give us more details?
You can use the <pre> (preformatted) tag so that html and carriage returns are represented without doctoring the field input
html:
<input id="text" type="text" />
<pre id="outputtext"></pre>
jQuery:
$(document).ready(function () {
$('#text').keyup(function () {
$('#outputtext').html($(this).val());
});
});
I need to display on a webpage a div when a user clicks on a button.
Does someone know how to do it ?
My code so far :
<body onload ="init()">
<input type="button" value="Display the div tree" onclick="check();" />
<script ="text/javascript">
function check() {
// I'd like to display on the page the div tree
}
</script>
<div id = "tree" style="display:none"> // I don't want to display it unless the user clicks on the button "Display the div tree"
</div>
</body>
thanks,
Bruno
document.getElementById('tree').style.display='';
Include this in your check function
i'm not sure if i understood the question, this seems a bit too easy:
function check() {
document.getElementById('tree').style.display=''; // or display='block';
}
EDIT :
the reason this dooesn't work for you is an error in your code. please change this line:
<script ="text/javascript">
to
<script type="text/javascript">
and everything wiill be fine. also, you should place the script in the head of your document, but thats not absolutely neccessary.