Solved

Change Jquery variable on click

Posted on 2010-09-14
10
504 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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:shinuq
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now