2

Please help me to combine two scripts of jquery. AT the moment they are not compatible to each other. What can I do to make them both friendly.

Jquery:

<script>
function edit_product() {
var id = $('.DIVY.panel3').attr('id');
alert (id);
</script>

<script>
  $(function(){

    $('.DIVY').click(function(){
        $('.DIVY.panel3').removeClass('panel3').addClass('panel2'); 
        $(this).removeClass('panel2').addClass('panel3'); 
    })
})
</script>

html:

<div id="1" class="DIVY panel2" >click me please!</div>
<div id="2" class="DIVY panel2" >click me please!</div>
<div id="3" class="DIVY panel3" >click me please!</div>
<div id="4" class="DIVY panel2" >click me please!</div>

<div ><a href="#" onclick="edit_product()">Check ID</a></div>

For better visualization, I put this code to My_jsfiddle
Thank you for your efforts!

1 Answer 1

2

Your code works on jsfiddle, just change the execution to No wrap - <in head> on the Frameworks & Extensions panel, see http://jsfiddle.net/IrvinDominin/CevcR/4/

But don't mix jQuery and inline js code if you can, you lose clarity in the code.

Assign an id to your anchor element like editMe and set its click function, your code can look like:

 $(function () {

     $('.DIVY').click(function () { // when a .myDiv is clicked
         $('.DIVY.panel3').removeClass('panel3').addClass('panel2');
         $(this).removeClass('panel2').addClass('panel3');

     })

     $('#editMe').click(function () { // when a .myDiv is clicked
         var id = $('.DIVY.panel3').attr('id');
         alert(id);
     })

 })

Demo: http://jsfiddle.net/IrvinDominin/CevcR/2/

Sign up to request clarification or add additional context in comments.

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.