Solved

Change Jquery variable on click

Posted on 2010-09-14
10
505 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
preventing spam with mailto 22 29
jquery datatables, asp.net MVC 9 38
does post require a form or curl to be post 4 29
How can i  prevent default  Submision? 6 24
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now