Solved

Change Jquery variable on click

Posted on 2010-09-14
10
512 Views
Last Modified: 2013-12-12
I'm looking for a way to change a variable in javascript, based on which of two buttons is clicked:

<script type="text/javascript">      
      RPXNOW.default_provider = 'facebook';  <--- need this to changed based on which was clicked
        RPXNOW.overlay = true;
        RPXNOW.language_preference = 'en';
</script>


The 2 buttons are images:
<a href="link"><img id="facebook" src="image"/></a>
<a href="link"><img id="twitter" src="image"/></a>
0
Comment
Question by:whatshakin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 22

Expert Comment

by:kadaba
ID: 33674551
this way..

$(document).ready(function(){
    $("#facebook").click(function(){
		 RPXNOW.default_provider = "facebook";
	});
	$("#twitter").click(function(){
		 RPXNOW.default_provider = "twitter";
	});
});

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33674552

<script type="text/javascript">      
      RPXNOW.default_provider = 'facebook';  <--- need this to changed based on which was clicked
        RPXNOW.overlay = true;
        RPXNOW.language_preference = 'en';

       $(document).ready(function(){
         $("#link1").bind("click", function(){
                RPXNOW.default_provider = 'facebook';
          });
         $("#link2").bind("click", function(){
                RPXNOW.default_provider = 'twitter';
          });
});
</script>


<a href="#" id='link1'><img id="facebook" src="image"/></a>
<a href="#" id='link1'><img id="twitter" src="image"/></a>
0
 

Accepted Solution

by:
whatshakin earned 0 total points
ID: 33674744
Ok, thanks for the help.  It was sort of a mix of these answers that worked.  The .click function would only work on the second time clicking for some reason so I changed it to hover and it works great:

$(document).ready(function(){
      $("#facebook").hover(function(){
                  RPXNOW.default_provider = 'facebook';
      });
      $("#twitter").hover(function(){
                  RPXNOW.default_provider = 'twitter';
      });
      RPXNOW.overlay = true;
      RPXNOW.language_preference = 'en';

});
0
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33674773
You need to give the onclick to the anchor tag, and either remove href attribute or make it to "#". then it will work on first click itself.
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 33674865
If you have a link that only performs a javascript function, you should always return false at the end of the click handler.  It's just good practice
<div id="links">
<a href="#" id='link1'><img id="facebook" src="image"/></a>
<a href="#" id='link1'><img id="twitter" src="image"/></a>
</div>

<script type="text/javascript">
$('#links a').bind('click', function() {
    RPXNOW.default_provider = $(this).find('img').attr('id');
    return false;
});
</script>

Open in new window

0
 
LVL 22

Expert Comment

by:kadaba
ID: 33674909
It's just good practice
Its more than that, you must have it to not evaluate href
Not good to have same ids.


0
 
LVL 22

Expert Comment

by:kadaba
ID: 33674960
It depends on your links, what do they do? I mean the hrefs ..
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 33675037
sorry about duplicate IDs, copy and pasted from someone else's comment... that's what I get for copying and pasting ;)

And my reasoning for "it's good practice" was because it's not really necessary if you are using hashes in the href.
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 33679615
<div id="links">
<img id="facebook" src="image"/>
<img id="twitter" src="image"/>
</div>

Try without using the href, since jquery is confused on the request that is coming from the href or the image, hence you need to click it twice.

Hope this helps

0
 
LVL 2

Expert Comment

by:WilliamStam
ID: 33711321
im thinking you better off with

<a href="javascript:;" rel="facebook" class="links">...</a>
<a href="javascript:;" rel="twitter" class="links">...</a>


$("a.links").click(function(){

 RPXNOW.default_provider = $(this).attr("rel")

});
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question