<script>
$(document).on('click','a',function() {
$.get('updateusers', function(data) {
$('#div1').html(data);
});
});
</script>
This script works great for updating particular div, but the problem is that it can be activated by every link on the page. This script even affected by <input type="submit"> tag.
For example, let page contains two link
<a id="link1" href="#">Update the link1 div</a>
<a id="link2" href="#">Update the link2 div</a>
So how to change the script to be affected only by specific link, for example only by link1 ?
Try something like this:
$(document).on('click','a',function(e) {
if ($(e.target).attr('id') == 'link1') {
$.get('updateusers', function(data) {
$('#div1').html(data);
});
}
if ($(e.target).attr('id') == 'link2') {
$.get('updateusers', function(data) {
$('#div1').html(data);
});
}
});
This will do a check to see if the clicked element has the ID you want.
Related
i know simple java script function on click
$(document).ready(function(){
$(".click").click(function(){
$(".fade", this).fadeToggle();
});
});
but i want that on click one div appears and second one closes . like on this page http://www.wpbeginner.com/free-wordpress-blog-setup/, but on viewing source i could not find any code . Don't worry this link doesn't belong to me or my website
I think that should do it
$(document).ready(function(){
$('.click').click(function(){
if ($('.first').is(':visible')) {
$('.first').css('display', 'none');
$('.second').fadeIn(2000);
} else {
$('.second').css('display', 'none');
$('.first').fadeIn(2000);
}
});
});
http://jsfiddle.net/57VaZ/
Assuming your html looks something like this:
<a class="click">click</a><br />
<a class="fade1">fade1</a><br />
<a class="fade2">fade2</a><br />
Make sure the one that you want to show on click is hidden by default:
.fade2 { display:none }
Then do the following in jQuery:
$(document).ready(function(){
$(".click").click(function(){
$(".fade1, .fade2").fadeToggle();
});
});
Here is a fiddle demo
I wonder why this snippet works on chrome (Version 31.0.1650.57 m) and not in firefox (25.0)
<span class="chat-user" id="directeur">
<a class="user" user="directeur" room="" href="#">
directeur
</a>
</span>
<script>
$("a[class=user]").click(function(event) {
alert();
});
</script>
I click on the "directeur" link, on chrome, I get an alert window, nothing happens in firefox.
Edit, I found out what was realy the problem
I was changing the DOM in another function:
window.setInterval(function() {
$.get("url",
function(data) {
data.forEach(function(entry) {
if (0 != $( ".chat-connected" ).find($("#"+entry.user)).size()) {
$("#"+entry.user).find("a").css("color", "red");
$("#"+entry.user).find("a").attr("room", entry.room);
}
});
});
}, 30000);
That change was modyfing the DOM and the click jquery function was not applied anymore to the <a> attribute.
I don't know what's causing differences between browsers. Perhaps the missing < on the </script> tag? Anyway, you should really be using
$("a.user").click(function(event) {
alert();
});
Try this instead :
<span class="chat-user" id="directeur">
<a class="user" user="directeur" room="" href="#">
directeur
</a>
</span>
<script>
$("a.user").click(function(event) {
alert("It works !");
});
</script>
Using the CSS selector when it exists is much more straightforward...
The solution was to change $("a.user").click(...) with $("a.user").on("click", ...)
I want to call 2 different jquery functions to hide links
<a href="">.zip file, first link
</a>
<script>
$("a").click(function() {
location.href="first.location";
return false;
});
</script>
<a href="">.tar.gz file, second link
</a>
<script>
$("a").click(function() {
location.href=="second.location";
return false;
});
</script>
How can I call the 2 functions so that I call the first one clicking the first link and the second one clicking the second link?
Thanks a lo
This is not the best solution. For best results you might want to restructure your html and add some sort of classes and IDs to the links or their parent to identify them. But this will work
For the first link
$("a:eq(0)").click(function() {
location.href="first.location";
return false;
});
and for the second link
$("a:eq(1)").click(function() {
location.href=="second.location";
return false;
});
If you set the href in the markup there is no need for JQuery or Javascript.
<a href="first.location">.zip file, first link
</a>
<a href="second.location">.tar.gz file, second link
</a>
You can use the :eq() Selector here like:
// Clicking the first link
$("a:eq(0)").click(function () {
location.href = "first.location";
return false;
});
// Clicking the second link
$("a:eq(1)").click(function () {
location.href = "second.location";
return false;
});
Like it has already been suggested the best way to do it is to have different id's for these a tags. But if for some reason you don't want to assign ids(why on earth would you do that?) you could do the following:
Wrap the anchor tags in a div and give it an id like this
<div id="myDiv">
First Link
Second Div
</div >
Then use jQuery to do the linking:
<script>
$(function(){
$("myDiv").children(a:first-child).click(function(){
// Do stuff here
});
$("myDiv").children(a:last-child).click(function(){
// Do stuff here
});
});
</script>
you can introduce an id attribute to your links. and then trigger the events based on the id of the element.
<a href="" id='link1'>.zip file, first link
</a>
<script>
$("#link1").click(function() {
location.href="first.location";
return false;
});
</script>
<a href="" id='link2'>.tar.gz file, second link
</a>
<script>
$("#link2").click(function() {
location.href=="second.location";
return false;
});
</script>
Give link in html(href)
$("a").click(function()
{
location.href = $(this).attr('href');
return false;
});
I think this might help:
<a id="first" href="">.zip file, first link</a>
<script>
$("first").click(function() {
location.href="first.location";
return false;
});
</script>
<a id="second" href="">.tar.gz file, second link </a>
<script>
$("second").click(function() {
location.href=="second.location";
return false;
});
</script>
$("a:eq(0)").click(function() {
location.href="first.location";
return false;
});
$("a:eq(1)").click(function() {
location.href=="second.location";
return false;
});
I have a question regarding the double submission.
I have a multiple <a href = "">.
I want to disables all the <a href=""> if i click in one of the <a href= "">
Code:
<a href="dashboard.php" id ="submitID" class="submit" >Dashboard </a>
<a href="orderList.php" id ="submitID" class="submit" >Order List</a>
New Order
First, please fix your ids to be unique.
If you're using jQuery versions 1.4.3+:
$("a.submit").click(function() {
$("a.submit").bind('click', false);
});
If not, bind function() { return false; }. Then you can also
$("a.submit").unbind('click')
when you want them to work again.
Welcome to Stack Overflow.
First of all, you should never have multiple DOM elements with the same ID.
Second of all, set a variable in a bind to the submit class (the bind is using jquery), and flip it if you submit.
Include jquery with a script tag and then wrap your javascript in document ready
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript>
$(document).ready(function () {
$('.submit').bind('click', function () {
var isSubmitted = false;
if (isSubmitted === false) {
$.get($(this).attr('href'), function () {
isSubmitted = true;
});
}
});
});
</script>
This is of course assuming you want some ajax style functionality. If not, you shouldn't really be worried if you have a link since you'd be posting to a new page
Jquery:
var count=0;
$(".submit").click(function(){
if(count>0){
return false
}
++count;
});
HTML
<a href="dashboard.php" id ="submitID1" class="submit" >Dashboard </a>
<a href="orderList.php" id ="submitID2" class="submit" >Order List</a>
New Order
var submitStatus = false;
$('a.submit').click(function(e){
if (!submitStatus) {
alert('clicked');
submitStatus = true;
} else {
e.preventDefault();
}
});
You can try it here: http://jsfiddle.net/p8a5s/
And dont use the same IDs for different DOM elements, of course
I'm trying to set up a javascript click detector.
If the person clicks part 2 without clicking 'link1' or 'link2' an alert will pop up telling him to click one of the links.
If the person clicks a link he should be allowed to the thankyou page.
My problem is it doesn't ever let the user pass even if he clicked one of the links.
<a id="postos" href="http://link1.com">Link1</a><br>
<a id="postos" href="http://link2.com">Link2</a><br>
<script src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script>
$("#postos").click(function () {
$("#linkos").attr('href', 'http://example.com');
});
</script>
Part 2 Click <a id="linkos" href='javascript:window.alert("please click a link");'> Here </a>
You can't have two elements with the same id.
Change it to a class:
<a class="postos" href="http://link1.com">Link1</a><br>
<a class="postos" href="http://link2.com">Link2</a><br>
$(".postos").click(function () {
$("#linkos").attr('href', 'http://example.com');
});
It is invalid to have multiple times the same ID.
Add a class to your first links and select them using the class:
<a class="link" href="http://link1.com">Link1</a><br>
<a class="link" href="http://link2.com">Link2</a><br>
$(".link").click(function (e) {
$("#linkos").attr('href', 'http://example.com');
});
DEMO
Id should be unique, so better user classes:
<a class="postos" href="http://link1.com">Link1</a><br>
<a class="postos" href="http://link2.com">Link2</a><br>
JS
$(".postos").click(function () {
$("#linkos").attr('href', 'http://example.com');
});
id must be unique, change from id to class:
HTML:
<a class="postos" href="http://link1.com">Link1</a><br>
<a class="postos" href="http://link2.com">Link2</a><br>
<a id="linkos" href='http://example.com'> Here </a>
jQuery:
$('.postos').click(function(){
$(this).data('clicked', 'yes');
})
$('#linkos').click(function(){
if ($('.postos[data-clicked="yes"]').length == 0)
{
alert("please click a link");
return false;
}
});
Use this:
<script>
var next = function () {
var confirmWindow = confirm("You want to go to a_website.");
if (confirmWindow) {
return true;
}
return false;
};
</script>
Text here...
Once the button is clicked (onclick attribute), it'll call a function.
Then the function will just ask the user if they want to go there.
The true or false will be returned depending on what button it clicks.
Then the if statement will just see if it's true or false.
Then the correct code will be ran.
Is this what you wanted?