Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change Jquery variable on click

Posted on 2010-09-14
10
Medium Priority
?
520 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:Gurvinder Pal Singh
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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