0

I have a scenario like this:

for (var k in active) {
        notifications.append(
            '<a href="javascript:toast();">' +
            '<div>' +
            '<input class="phone_number" type="hidden" id='+active[k].phone_number+' value='+active[k].phone_number+'>'+
            '</input>'+
            '<span class="notify bg-blue">' + '<i class="fa fa-clock-o"></i>' + '</span>' +
            '<span>' +
            '<span>Sim ' + active[k].phone_number + ' Expires in ' + active[k].expiry_count + 'days</span>' +
            '<br/>' +
            '<span class="time">Just Now</span>' +
            '</span>' +
            '</div>' +
            '</input>'+
            '</a>');
    }

i created dynamic elements using jade template engine. here i need to identify each <a> click and i nedd to get the value of hidden field.

function toast() {
    var grade;
    //var phone_number=document.getElementById('phone_number').value;
    $.each($('.phone_number'), function () {
                    grade = $(this).val();
        alert(grade);
    });
}

by doing this i got a loop of value. which i didn't want. i need single item value. how to solve this ?

3 Answers 3

3

pass this to the toast

 '<a href="javascript:toast(this);">' +

and change the toast method to

function toast( thisObj ) 
{
    var grade = $( thisObj ).parent().find( '.phone_number[type="hidden"]' ).attr( "value" );
    alert( grade );
}
Sign up to request clarification or add additional context in comments.

6 Comments

can you put a console log statement in the toast and tell me what is the grade value?
bro the toast function not invoking.
was it invoking earlier? Can you create a snippet so that current issue can be replicated and verified?
bro now its invoked, but got no value. i got undefined (i am using jade templating engine),
I don't think Jade has a role to play once if you are appending markup to an element this way. Using @krishnar's code, can you replicate the issue you are facing?
|
1

notifications = $(".notifications")

active = [{ phone_number: "982334",expiry_count: "1"},
         { phone_number: "982334",expiry_count: "1"}]
var k;
for (var k in active) {
         
        notifications.append(
            '<a href="javascript:toast();">' +
            '<div>' +
            '<input class="phone_number" type="hidden" id='+active[k].phone_number+' value='+active[k].phone_number+'>'+
            '</input>'+
            '<span class="notify bg-blue">' + '<i class="fa fa-clock-o"></i>' + '</span>' +
            '<span>' +
            '<span>Sim ' + active[k].phone_number + ' Expires in ' + active[k].expiry_count + 'days</span>' +
            '<br/>' +
            '<span class="time">Just Now</span>' +
            '</span>' +
            '</div>' +
            '</input>'+
            '</a>');
    }
    
    
      $(document).on('click', 'a', function(){
         
          alert($(this).find('.phone_number').val())
    })
      
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



<div class="notifications"></div>

Its simple.Try this

    $(document).on('click', 'a', function(){
          alert($(this).find('.phone_number').val())
    })

7 Comments

@AnonymousObject try this
bro its working but, it invoking multiple times and some times i got undefined value.
@AnonymousObject try now. I given sample snippet as well.
@AnonymousObject I hope you got the solution. If you found it useful please upvote and accept answer.Thank YOU
bro i am using jade template, i dont know what the error is, i still got multiple alert messages on single click .
|
0

Send the toast function the k you are iterating over

'<a href="javascript:toast(' + k + ');">'

then use k in your function

function toast(k) {
   //use k (maybe select by id)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.