zhuanzhou
zhuanzhou

Reputation: 2443

how to use the dynamic id in javascript or jquery?

php code: the following code is

<div id="code'.$row->nid.'" class="cptext" rel="'.$url.'">'.$row->node_value.'</div> 
<div id="cd'.$row->nid.'" style="display: none; border: 1px solid #ccc; ">test test</div>';

the above code will generate id's like this: code1, code2, code3, code4, code5, ... now, in my js file i want to use the id value.

function init() {   
    clip.setHandCursor( true );
        $('this i want to put the id value in which from the above').mouseover( function() {
  }

Upvotes: 0

Views: 1569

Answers (4)

kapa
kapa

Reputation: 78671

You can access them in jQuery by using the Attribute Starts With selector. My example will select the ones with an ID that starts with code.

$('[id^="code"]').whatever();

jsFiddle Demo

But I'd say it would be nicer and more logical to give these elements a class they share, so you could easily do selections.

<div id="code'.$row->nid.'" class="cptext code"...

and then just $('.code')....

Upvotes: 1

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

Using jquery, $('#code1').text() will give you the text value.

UPDATE based on updated question:

Since you already have a class set:

$('.cptext').mouseover( function() {...} );

Upvotes: 1

DoXicK
DoXicK

Reputation: 4812

var theId = $(this).attr('id');

You can just look up the jquery documentation (or google for instance?)

Upvotes: 1

Headshota
Headshota

Reputation: 21449

<script type="text/javascript">
    var myid = <?php echo $row->nid; ?>;
</script>

Upvotes: 0

Related Questions