sharataka
sharataka

Reputation: 5132

How to return value to console in javascript?

I am trying to write some javascript that will return the value of toasty.png and bready.png when the user clicks on the respective text. I'm able to return "Toast" and "bread" but not the other text. Any advice?

<script>
    $(document).on('vclick', '.changePageButton', function() {
        console.log(this.text);
        //console.log(value within the image)
    });
</script>

<a class="changePageButton" value="Toast" data-transition="slide">
    <input type = "hidden" name = "image" value = "toasty.png">
    <input type = "hidden" name = "video" value = "video1.mpg">
    test
</a>

<a class="changePageButton" value="bread" data-transition="slide">
    <input type = "hidden" name = "image" value = "bready.png">
    <input type = "hidden" name = "video" value = "video2.mpg">
    test
</a>

Upvotes: 0

Views: 3723

Answers (3)

Ch Tsogbadrakh
Ch Tsogbadrakh

Reputation: 31

Form tag usable for get element

<script>
 $(document).on('vclick','.changePageButton', function() {
  var frm = document.getElementById('ID');
  // jQuery frm = $("#ID")
  console.log(this.text);
  console.log(frm.image.value[0]);
  console.log(frm.image.value[1]);
  // or you can use loop  FOR, WHILE etc
 });
</script>
<form id="ID">
    <a class = "changePageButton" value = "Toast" data-transition="slide">
        <input type = "hidden" name = "image" value = "toasty.png">
        test
    </a>

    <a class = "changePageButton" value = "bread" data-transition="slide">
        <input type = "hidden" name = "image" value = "bready.png">
        test
    </a>
 </form>

Upvotes: 0

rink.attendant.6
rink.attendant.6

Reputation: 46317

// Also did you mean "click"?
$(document).on('click', '.changePageButton', function () {
    var inputs = {};

    console.log(this.text);

    $(this).children('input').each(function (v) {
        inputs[$(this).prop('name')] = $(this).val();
    });

    console.log(inputs);
    console.log(inputs.image);
    console.log(inputs.video);
});

Upvotes: 2

Sonu Sindhu
Sonu Sindhu

Reputation: 1792

try this

$(document).on('vclick','.changePageButton', function() {

     console.log($(this).find("input[type='hidden']").val());

     // if you want according to hidden field name
     console.log($(this).find("input[name='image']").val());

});

I hope it will help

Upvotes: 0

Related Questions