1

can anyone please tell me how to send div id through button click java-script parameter. Here in the below code the main div id 'ricId_1' and the class name "ric_class1 rico_class0 ric1_class1 ric2_class3" is created dynamically has to be send through javascript method cancel('divid') in the button click.The div id is not static.

<div id="ricId_1" class="ric_class1 rico_class0 ric1_class1 ric2_class3" style="width: 730px; left: 309px; top: 71.5px; z-index: 13000; display: block;">
        <div class="ricTitle">
            :
        </div>

        <div class="ricModal ng-scope" style="height: auto;">
            :
        <div>

        <div>
            <div ng-controller="Manage" class="ng-scope">
                <div class="ricG ricAlign">
                    <div class="ricGrid"><div class="ricGridTable">
                     :
                    </div>
                </div>
            </div>
        </div>

        <div align="center" class="row btn-group">
            <button onclick="cancel('divid')" class="ricButton" type="button" id="sss" ric:loaded="true">Close</button>
        </div>

    </div>
2
  • the divid should be equal to the clicked button id? Commented Feb 12, 2014 at 9:22
  • @BeNdErR actually divid is been created dynamically if it is static we can assign . Commented Feb 12, 2014 at 9:24

4 Answers 4

3

Pass the button to cancel and use that id to get the closes ancestor with class ric_class1 rico_class0 ric1_class1 ric2_class3 having id ricId_1

Live Demo

Html

<button onclick="cancel(this)" class="ricButton" type="button" id="sss" ric:loaded="true">Close</button>

Javascript

function cancel(btn)
{
     ricId_1 = $(btn).closest('ric_class1 rico_class0.ric1_class1 ric2_class3');
     //or
     ricId_1 = $(btn).parent().parent();

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

4 Comments

but here ric_class1 rico_class0.ric1_class1 ric2_class3 is also dynamic
You can use parent().parent() then
there all div's are properly closed jsfiddle.net/8F8Mr/2/.....still giving undefined
1

Try this

HTML

<div id="ricId_1" class="ric_class1 rico_class0 ric1_class1 ric2_class3" style="width: 730px; left: 309px; top: 71.5px; z-index: 13000; display: block;">
  <div align="center" class="row btn-group">
    <button class="ricButton" type="button" id="sss" ric:loaded="true">Close</button>
  </div>
</div>

jQuery Code

$('.ricButton').on('click', function(){
    var getId = $(this).parent().parent().attr('id');
    alert(getId);
});

Demo

3 Comments

because your divs are not close properly. check you html code.
how bout this here all div's are properly closed jsfiddle.net/8F8Mr/2/.....still giving undefined
in this you have added two more parent for $(this) object. that's why it's showing undefined. One more thing in current fiddle no need to close the br tag. it's self closing tag </br> not '<br style=""></br>'
0

Use jQuery:

$("#sss").click(cancel(divid ));

Comments

0

pass this to the function call and retrieve the id later

JSFIDDLE http://jsfiddle.net/PrX52/

HTML

<button onclick="cancel(this)" class="ricButton" type="button" id="sss" ric:loaded="true">Close</button>

JS

function cancel(el){
    alert(el.id);
};

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.