1

How can I give the ID value to Javascript in same page.

Example data :

<a href="" id="ctextarea<?php echo $uid; ?>"/>

JS :

function recountss()
        {
            var maxlen=280;
            var current = maxlen-$('#ctextarea').val().length;
            $('.counters').html(current);


            if(current<0 || current==maxlen)
            {
                $('.counters').css('color','#D40D12');
                $('input.comment_button').attr('disabled','disabled').addClass('inact');
            }
            else if (!$.trim($("#ctextarea").val())) {
                $('input.comment_button').attr('disabled','disabled').addClass('inact');
            }
            else
                $('input.comment_button').removeAttr('disabled').removeClass('inact');


            if(current<10)
                $('.counters').css('color','#D40D12');

            else if(current<20)
                $('.counters').css('color','#5C0002');

            else
                $('.counters').css('color','#C0C0C0');
        }

I tried using get class, but it's effect for 1 data not for many data. Have idea ?

Thanks.

2 Answers 2

2

This should work :

<script type="text/javascript">
    var id = "ctextarea<?php echo $uid; ?>";
</script>

In your javascript, you can do :

var current = maxlen-$(id).val().length;

But you should use an other name for your variable.

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

4 Comments

So how can I set that in my JS ?
@nagini that does set it in JS. Notice the script tags
Still confused how can set that ? :(
When you are in a script tag, you are writing javascript. So i just use php to generate a javascript value and i store it in the id variable. Then in your javascript code, you can use the id variable.
0

Try data- attributes:

<a href="somewhere.php" data-id="<?php echo $uid; ?>" id="link">Link</a>

In JS you can do the following:

document.getElementById('link').getAttribute('data-id');

This stuff is perfectly valid in HTML5 - but even older browsers just don't care.

1 Comment

I was about to write a similar answer, but I think that the anchor tag is just a dummy element to store a variable.

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.