2

I have 2 different links with inline js added.

I would like to add both to a single inline onclick.

Here are the two separate one:

<a href="#" onclick=\'$("#id").toggle();\'>

<a href="#" onclick="myvar = '1';">

And this is what I would like to do:

<a href="#" onclick="$("#id").toggle();myvar = '1';">

How could I do this?

12
  • You want this ? <a href="#" onclick="$('#id').toggle();myvar = '1';">. Did you see you have problems with quotes ? Commented Sep 25, 2012 at 13:24
  • 1
    What actually are you trying to do? Why do you separate JavaScript code into several inline event handlers? Commented Sep 25, 2012 at 13:24
  • Just write this: <a href="#" onclick="$('#id').toggle();myvar = '1';"> Commented Sep 25, 2012 at 13:25
  • First of all: there's no jQuery in here. And second: don't do this. Separate your HTML and the client code. Bind event handlers not in the HTML. You'll thank me. Commented Sep 25, 2012 at 13:26
  • 3
    I could answer the question by telling you to escape the quotes in $("#id"), but that would mean that you'd continue to use inline javascript, which is pretty much always the wrong approach. Commented Sep 25, 2012 at 13:27

6 Answers 6

6

you can use

$("a").click(function(){
   $('#id').toggle(); myvar = '1';
 })
Sign up to request clarification or add additional context in comments.

Comments

4

Attach your event handlers in javascript (why?), then it becomes trivial:

HTML:

<a href="#" id="myAnchor" />

JS:

document.getElementById('myAnchor').onclick = function () {
    $("#id").toggle();
    myvar = '1';
};

Comments

3

Exactly like you have, but with syntax errors fixed...

<a href="#" onclick="$('#id').toggle(); myvar = '1';">

Incidentally, be ready for a slew of people telling you not to use inline code.

3 Comments

I don't use inline frequently but in this case I want to though :)
@dystroy - please don't judge people by your own standards. I posted this answer before your comment, but you don't see me accusing you of foul play.
@Satch3000 - I understand. You do it the way you want to do it, that's why I answer your question as asked :)
2

simply use this

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

$("#id").toggle(); myvar = '1';

 })

Comments

1

... You did it, basically. You just have to unify how you're declaring your strings and you're all set:

<a href="#" onclick="$('#id').toggle();myvar = '1';">

Voila.

4 Comments

I didn't downvote but this comes a few minutes after comments and answers already saying it.
+1 ( because of down vote ). Technically it does answer the question. However it is a bad practice to keep HTML and JavaScript not separated.
It might now be best practice but totally legal so why not use it? :)
Because people are under the mistaken impression that if you don't put any JS tags in your HTML, that will somehow make it totally independant of the JS attached to it. Never mind that most applications are inexorably tied to the JS behind them, and using onClick to call functions will not only make the purpose of the page more clear while reading just the HTML. Additionally, the premise is flawed because people love to assign a unique ID to every element and then use $('#id').click(dosomething); Meaning the html and JS are now tied in the far more reusable section of the code.
1

Can't you just do what you are doing in javascript?

$(document).ready(function(){
    $('#id').toggle(function(){
        myVar = 1;
    });
});

... or something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.