samedi 27 octobre 2018

jQuery Radio Buttons If/Else

When the user clicks on an image, I'm trying to append to some nearby div ("image_output"), the filename (if they clicked that radio button) or the alt attribute value (if they clicked on the description radio button). I know I will need to use the 'this' attribute for this part. Right now, I have an idea of what to write, but I'm struggling. I know I added lines that aren't code I was using that to get an idea of what the layout would be but I don't think it is right. Right now, nothing will open in a new tab.

<script>
$(document).on('dblclick', 'img', function () {
  var imgurl = window.location.pathname +'/'+ $(this).attr('src');
  window.open(imgurl, '_blank');
  $("input[name=user-type]:radio").click(function () {
    if ($('input[name=user-type]:checked').val() == "File Name") {
    window.open(imgurl, '_blank', $(this).attr('src'));
  else 
    window.open(imgurl, '_blank', $(this).attr('alt')).alt;
})
</script>

HTML:

<div id="main_content">

<div id="radio_images">
File Name   <input type="radio" name="imageselect" value="filename">    <br>
Description  <input type="radio" name="imageselect" value="description" checked>    <br>
</div>

<div id ="image_output">
</div>

<div id="images">
<h3>Some Images</h3>
    <p><img src="firetruck.jpg" alt="pic of truck">  |
    <img src="baseball.jpg" alt="pic of baseball" >  |
    <img src="soccer_ball.jpg" alt="pic of soccer ball" >
    </p>
</div><!-- end of 'images' div -->
<hr>

</div><!-- end 'main_content' div -->

Aucun commentaire:

Enregistrer un commentaire