Solved

Change Jquery variable on click

Posted on 2010-09-14
10
507 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

749 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