Hi ,

I need to know how to pass html element into jquery function.

My sample html code is given below:

<html>
<head>
<script src='../../core/js/jquery-1.4.2.min.js'></script>
<script src='../../core/js/jquery-ui-1.8.1.min.js'></script>
<script src='../../core/js/extensions.js'></script>
<script src='../../core/js/prot-lib.js'></script>
</head>
<body>
<table>
 <tr>
  <td class='r1'>
    <chl class="chl">
            <chli class="menu-list">
              <li id="s0" class="item" onClick="handle('s0')">NJ</li>
              <li id="s1" class="item" onClick="handle('s1')">TX</li>
            </chli>
          </chl>
  </td>
  <td class='r1'>
    <chl class="chl">
            <chli class="menu-list">
              <li id="s0" class="item" onClick="handle('s0')">NJ</li>
              <li id="s1" class="item" onClick="handle('s1')">TX</li>
            </chli>
          </chl>
  </td>
 </tr>
</table>
</body>
</html>

Jquery function is given below:

function handle(id){

    var text = $('#'+id).text();    
    $('#'+id).closest('chl').find('#id').css("color","black").val(text);

}

I want to pass the html element also in handle function. Reason is the above is accessing first element always when i select second list element in html. The li id's are same for both the html elements.
Is there anyway I can pass the html element itself to the function handle() along with id?

Its immediate requirement. Any help appreciated.

Thanks in Advance,
VC

Pritaeas,

Thank you for the reply. That helped. Is there a way to get random numbers to set it as id's in jquery?

thank you,
VC

I have another problem. After using 'this' in function, I am using it to set the value of its parent's parent to a string using val(string) function. It is neither showing any error nor working.

code:

$(this).parent().parent().find('#input-value').css("color","black").val(text);

I am able to get its value using text() function but val() is not working.
What could be the reason for this?

Thanks,
VC

Is there a way to get random numbers to set it as id's in jquery?

What do you want to do ? Do you mean something like this:

$('li').each(function(){
    var newId = 's' + Math.floor(Math.random() * 100);
    $(this).attr('id', newId);
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.