Call jquery tag in javascript function [closed] - javascript

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 4 years ago.
Improve this question
<script>
function addMoreAns() {
$(".add").append("<h1>Hi</h1>")
}
</script>
It is my jquery tag in javascript function.
jquery tag don't work under javascript function

You must have three things for your code to work:
You must make sure to have the JQuery library referenced prior to your use of any JQuery code.
In the HTML, you must have an element that is allowed to contain content with an add class.
You must invoke your function somehow. Just writing a function isn't the same thing as running it. You have to decide when the function should run and set up your code to invoke it at that point.
In the example below, the function is invoked every time the button is clicked.
document.querySelector("input[type=button]").addEventListener("click", addMoreAns);
function addMoreAns() {
$(".add").append("<h1>Hi</h1>")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Click to Add">
<div class="add"></div>

You need to load the jQuery library in the head of your page
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

Related

How do I switch html pages with JavaScript if I don't have the Url [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 17 hours ago.
This post was edited and submitted for review 17 hours ago.
Improve this question
I have them both as HTML files in my repo. The button is just meant to switch from pages for now. I will add the function for the inputs and conditions later. I just want to get in working before I add anything. There are also no errors that or popping up. I cannot use a tag as I need to change the URL to pass data through it, so I need to have it in JS so I can change it with the data from the input.
The plan is to have the user put in their name in a text input, and have it inside the Url, then have the 2nd page process the name and output a text and photo.
This is the code that I have tried.
const nameButton = document.getElementById("nameBtn");
$(nameButton).click(function(){
preventDefault()
function change_page(){
window.location.replace("page-two.html");
};
change_page()
});
This is the Html:
<input class="font" type="text" id="inputName" value="Type in
your name..."></input>
<button class="fonts" id="nameBtn">Ready?</button>
</form>
</div>
</body>
<script src="scripts/script.js"></script>
</html>
Short answer, only replace the pathname
window.location.pathname = 'page-two.html'
In your case:
const nameButton = document.getElementById("nameBtn");
nameButton.addEventListener('click', function (event) {
event.preventDefault()
window.location.pathname = "page-two.html";
});

Onclick image the gallery opens [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on a mobile application, I am trying to make an image as a button: I have an image and I want to click on it, then when I click the mobile gallery opens so that I can upload a photo.
Any help please?
I think, you want to open file-uploader on image button click! Right?
If so, you can use pure javascript / jQuery as following:
HTML:
<div id="profilepic">
<input id="fileSelector" type="file" name="file" accept="image/*" capture="camera" />
<button id="clickButton" onClick="openFileUpload();"><img src="Images/profilepic.png" width="170" height="170"/></button>
</div>
For jQuery append this to head of your document:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Wrap following code in head too.
JQuery:
$(document).ready(function(){
$('#clickButton').on('click', function(){
$('#fileSelector').click();
});
});
OR Javascript:
function openFileUpload(){
document.getElementById('fileSelector').click();
}
http://jsfiddle.net/mas5qr1o/1/
See demo here:
jQuery DEMO
Javascript DEMO
Try the following jquery
$('#clickButton').on('click', function(){
$('#fileSelector').trigger('click');
});
DEMO

JavaScript/jQuery code in (blogger) post? [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 8 years ago.
Improve this question
How to paste JavaScript/jQuery code in blogger post?
Such like jsfiddle.net/tTman
It didn't work, don't know what I miss?
Please guide me the correct way to achieve my objective .
(Newbie learning)
My guess would be that you did not load the jquery library. Pasting the following code in the HTML view of a blog-post should work:
<img id="b" src="http://www.web-press.it/folder/servizi_cloud.jpg" id="b" style="position:absolute; top:50px"/>
<!-- Loading the required JQuery library -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function moveRight(){
$("#b").animate({left: "+=300"}, 2000,moveLeft)
}
function moveLeft(){
$("#b").animate({left: "-=300"}, 2000,moveRight)
}
$(document).ready(function() {
moveRight();
});
</script>
follow this path:
blogger.com -> Template -> Edit HTML
and past your javascript/jquery codes before </head>
<script type="text/javascript">
function moveRight(){
$("#b").animate({left: "+=300"}, 2000,moveLeft)
}
function moveLeft(){
$("#b").animate({left: "-=300"}, 2000,moveRight)
}
$(document).ready(function() {
moveRight();
});
</script>
see pic:
now!
go to Layout and add a gadget and input your HTML code into that!
<img id="b" src="http://www.web-press.it/folder/servizi_cloud.jpg" id="b" style="position:absolute; top:50px"/>
see pic:
Notice:
you must add Jquery libararies before your Jquery codes
if you want to show this effects only in posts, go to
blogger.com -> New Posts
and in edditor box, click on HTML and past your HTML Codes into that

How on one click another page div value change using JavaScript? [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 8 years ago.
Improve this question
I have two page page1.php, page2.php and one JS file Control.js.
In page1.php
HTML :
<img src="images/b.jpg" width="31" height="26" id="imgClick1" onClick="return changeImage1(this)" >
In Control.js
function changeImage1() {
document.getElementById("imgClick1").src = "images/b.jpg";
document.getElementById('num1').style.color = "#fff";
document.getElementById("text1").innerHTML = "Hello friend";
}
In page2.php
<div id="text1">
Hello world
</div>
So using JavaScript I am trying to write "hello friend" in the div "text1" of page page2.php when I click the image or the div of page1.php.
Is there any possible way to use JS to solve this problem?
Can we use any how document.getElementById("text1").innerHTML or something like that in the program?
I suggest you make use of the include function of php (provided you absolutely want to keep your text1 div on a separate page), like this on your page1.php file:
include('page2.php');
Make sure that when you do that, page1.php and page2.php are in the same folder, otherwise you may define a path to it like this:
include('/path/to/page2.php');

JQuery function not executing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
This should be super simple. The first code block works, but when I wrap it in a function and attempt to call the function, it doesn't work.
This Works:
$(document).ready(function() {
$("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
});
This Doesn't Work:
$(document).ready(function() {
function hideatstart() {
$("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
}
hideatstart();
});
UPDATE: After looking at the accepted answer, I was able to get working code. It involved not having any spaces between the lines of code, which would allow p tags to be inserted. Best practice would be to remove the JQuery to a separate script, which I will do. The following code works:
$(document).ready(function() {
function hideatstart(){
$("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
}
hideatstart();
});
I inspected the HTML on your live site and found that your snippet is outputted like this:
<div class="art-postcontent">
<!-- article-content -->
<!-- [snipped] -->
<p><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><br />
<script type="text/javascript">
$(document).ready(function() {</p>
<p> function hideatstart() {</p>
<p> $("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
}</p>
<p> hideatstart();
});
</script></p>
<!-- [snipped] -->
</div>
As you can see, WordPress's wpautop filter wrapped your lines inside paragraphs.
If you need to inject a script somewhere, you better do it with a plugin or a theme function and not by pasting the code in a WordPress post or widget. All regular WordPress content types are escaped to prevent such possibly malicious injections. It seems like you're using a custom art post type, which normally gets a similar treatment. There are ways to prevent the content from being escaped, but you should seriously consider moving your script code somewhere else.

Categories