javascript - Show / hide image function with remove classname -
i'm using following function display image on user clicks on in above div.
in event user clicks again on populated image (in div), remove image in div , have original image visible again.
<img id="<?php echo $id; ?>" src="img/.." url=".." name="..." onclick="changeimage(this)"/> function changeimage(item) { var url_img = $(item).attr('url'); var name_item = $(item).attr('name'); var item_id = $(item).attr('id'); document.getelementbyid('test').innerhtml += '<div style=\'display:inline-block\'><img src=' + url_img + ' url = ' + url_img + ' onclick=document.getelementbyid(' + item_id + ').classname=\'\'; /> ' + name_item + '</div>'; $(item).toggleclass('hideitem'); } .hideitem class display:none;
the onclick event in function not work (i trying remove classname) , i'm not able remove div image when clicked onclick event.
would have idea fix this?
thanks
there missing quotes , stuff in code.
for html:
<img id="myimgid" src="http://placehold.it/350x150" url="http://placehold.it/150x150" name="example dot com" onclick="javascript:changeimage(this)" /> <div id="test"> <p>this test</p> </div> this js:
changeimage = function (item) { var $item = $(item), $test = $('#test'), url_img = $item.attr('url'), name_item = $item.attr('name'), newdiv = $('<div/>').css('display', 'inline-block').text(name_item), newimg = $('<img/>').attr({'src':url_img, 'url':url_img}) .click(function(e){$item.removeclass('hideitem')}).prependto(newdiv); $test.append(newdiv) $item.toggleclass('hideitem'); }
Comments
Post a Comment