0

Hello again :) I have following code :

<script type="text/javascript" language="javascript">
$(document).ready(function () {
   $(function () {
       $('body').on('click', 'a.myclass', function () {
           sndcmpnm = $(this).attr("value");
           alert("From Small Function :" + sndcmpnm);
       });
   });
});
</script>

In the page at the bottom, I have a simple javascript with the line var deares = sndcmpnm; The funny thing is that no matter what I do, deares never gets the value of sndcmpnm. I tried another solution from SO, which said I must not put var infront of the variable but that also did not help. The alert alert("From Small Function :" + sndcmpnm); gives the right result. The java error code thingy says

ReferenceError: sndcmpnm is not defined

All I want is that deares can get the value of sndcmpnm. I don't see what I am doing wrong, please help me :(

[EDIT] Other code :

<script>
function discomp()
{
  alert("hello1");
  var deares = sndcmpnm;
  alert("hello2");
  alert("This is suppose to hold the sndcmpnm data :" +deares);
}
</script>
14
  • 3
    Where is your other code? Also, you have 2 DOM ready wrappers, you only need one. (And as a side note, jQuery is JavaScript - it's just a library) Commented Jul 28, 2014 at 1:15
  • 8
    The question title doesn't make any sense, jQuery is JavaScript. Commented Jul 28, 2014 at 1:15
  • please post full error source Commented Jul 28, 2014 at 1:19
  • hey @tymeJV : Added the other code :) Commented Jul 28, 2014 at 1:19
  • 2
    Seems like discomp is executed before the click event handler (and hence sndcmpnm is never set). Also, JavaScript has nothing to do with Java. Commented Jul 28, 2014 at 1:21

2 Answers 2

2

Just define sndcmpnm in the global scope of the script tag. (And you only need one document ready, $(function() { ... } is a shorthand for $(document).ready(function() { ... }.)

<script type="text/javascript" language="javascript">

var sndcmpnm = '';

$(function() {

    $('body').on('click', 'a.myclass', function() {

        sndcmpnm = $(this).attr("value") ;
        alert("From Small Function :" + sndcmpnm);

    });

});

</script>

<script>
function discomp() {
  var deares = sndcmpnm;
  alert("This is suppose to hold the sndcmpnm data: " + deares);
}
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, almost ! Thank you. Just that it gives output for deares now "undefined". If alert(sndcmpnm) then it shows "undefined".
It shouldn't. But when exactly is discomp() being called?
@jeanna.b: <a> elements don't have an attribute value.
@jeanna.b Sorry, had to set sndcmpnm to something for it not to be undefined anymore. It was accessible as it should though. And as @FelixKling pointed out, <a> tags don't have the attribute value. So what information is it you wish to get from the <a> tag exactly?
0

You could call the discomp function from within the event handler:

<script type="text/javascript" language="javascript">
function discomp(message)
{
  alert("This is suppose to hold the sndcmpnm data :" + message);
}

$(function () {
    $('body').on('click', 'a.myclass', function () {
        var sndcmpnm = $(this).attr("value");
        discomp(sndcmpnm);
    });
  });
});
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.